| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <linux/version.h> |
| #include <linux/module.h> |
| #include <linux/slab.h> |
| #include <linux/i2c.h> |
| #include <linux/types.h> |
| #include <linux/input.h> |
| #include <linux/delay.h> |
|
|
| #include "ais328dq.h" |
|
|
| #define I2C_AUTO_INCREMENT 0x80 |
| #define I2C_RETRIES 5 |
| #define I2C_RETRY_DELAY 5 |
|
|
| static int ais328dq_i2c_read(struct device *dev, u8 reg_addr, |
| int len, u8 *data) |
| { |
| int err = 0, tries = 0; |
| struct i2c_msg msg[2]; |
| struct i2c_client *client = to_i2c_client(dev); |
|
|
| reg_addr |= ((len > 1) ? I2C_AUTO_INCREMENT : 0); |
|
|
| msg[0].addr = client->addr; |
| msg[0].flags = client->flags & I2C_M_TEN; |
| msg[0].len = 1; |
| msg[0].buf = ®_addr; |
|
|
| msg[1].addr = client->addr; |
| msg[1].flags = (client->flags &I2C_M_TEN) | I2C_M_RD; |
| msg[1].len = len; |
| msg[1].buf = data; |
|
|
| do { |
| err = i2c_transfer(client->adapter, msg, 2); |
| if (err != 2) |
| msleep_interruptible(I2C_RETRY_DELAY); |
| } while (err != 2 && ++tries < I2C_RETRIES); |
|
|
| return (err != 2) ? -EIO : err; |
| } |
|
|
| static int ais328dq_i2c_write(struct device *dev, u8 reg_addr, |
| int len, u8 *data) |
| { |
| int err = 0, tries = 0; |
| u8 send[len + 1]; |
| struct i2c_msg msg; |
| struct i2c_client *client = to_i2c_client(dev); |
|
|
| reg_addr |= ((len > 1) ? I2C_AUTO_INCREMENT : 0); |
| send[0] = reg_addr; |
| memcpy(&send[1], data, len * sizeof(u8)); |
| len++; |
|
|
| msg.addr = client->addr; |
| msg.flags = client->flags & I2C_M_TEN; |
| msg.len = len; |
| msg.buf = send; |
|
|
| do { |
| err = i2c_transfer(client->adapter, &msg, 1); |
| if (err != 1) |
| msleep_interruptible(I2C_RETRY_DELAY); |
| } while (err != 1 && ++tries < I2C_RETRIES); |
|
|
| return (err != 1) ? -EIO : err; |
| } |
|
|
| static struct ais328dq_transfer_function ais328dq_tf_i2c = { |
| .write = ais328dq_i2c_write, |
| .read = ais328dq_i2c_read, |
| }; |
|
|
| static int ais328dq_i2c_probe(struct i2c_client *client, |
| const struct i2c_device_id *id) |
| { |
| int err; |
| struct ais328dq_acc_data *acc; |
|
|
| acc = kmalloc(sizeof(struct ais328dq_acc_data), GFP_KERNEL); |
| if (!acc) |
| return -ENOMEM; |
|
|
| acc->dev = &client->dev; |
| acc->name = client->name; |
| acc->bus_type = BUS_I2C; |
| acc->tf = &ais328dq_tf_i2c; |
| i2c_set_clientdata(client, acc); |
|
|
| err = ais328dq_acc_probe(acc); |
| if (err < 0) |
| goto free_data; |
|
|
| return 0; |
|
|
| free_data: |
| kfree(acc); |
| return err; |
| } |
|
|
| #if KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE |
| static void ais328dq_i2c_remove(struct i2c_client *client) |
| { |
| struct ais328dq_acc_data *acc = i2c_get_clientdata(client); |
|
|
| ais328dq_acc_remove(acc); |
| kfree(acc); |
| } |
| #else |
| static int ais328dq_i2c_remove(struct i2c_client *client) |
| { |
| struct ais328dq_acc_data *acc = i2c_get_clientdata(client); |
|
|
| ais328dq_acc_remove(acc); |
| kfree(acc); |
|
|
| return 0; |
| } |
| #endif |
|
|
| #ifdef CONFIG_PM_SLEEP |
| static int ais328dq_suspend(struct device *dev) |
| { |
| struct ais328dq_acc_data *acc = i2c_get_clientdata(to_i2c_client(dev)); |
|
|
| return ais328dq_acc_disable(acc); |
| } |
|
|
| static int ais328dq_resume(struct device *dev) |
| { |
| struct ais328dq_acc_data *acc = i2c_get_clientdata(to_i2c_client(dev)); |
|
|
| return ais328dq_acc_enable(acc); |
| } |
|
|
| static SIMPLE_DEV_PM_OPS(ais328dq_pm_ops, |
| ais328dq_suspend, |
| ais328dq_resume); |
|
|
| #define AIS328DQ_PM_OPS (&ais328dq_pm_ops) |
| #else |
| #define AIS328DQ_PM_OPS NULL |
| #endif |
|
|
| static const struct i2c_device_id ais328dq_ids[] = { |
| { AIS328DQ_ACC_DEV_NAME, 0 }, |
| { } |
| }; |
| MODULE_DEVICE_TABLE(i2c, ais328dq_ids); |
|
|
| #ifdef CONFIG_OF |
| static const struct of_device_id ais328dq_id_table[] = { |
| { .compatible = "st,ais328dq", }, |
| { }, |
| }; |
| MODULE_DEVICE_TABLE(of, ais328dq_id_table); |
| #endif |
|
|
| static struct i2c_driver ais328dq_i2c_driver = { |
| .driver = { |
| .owner = THIS_MODULE, |
| .name = AIS328DQ_ACC_DEV_NAME, |
| .pm = AIS328DQ_PM_OPS, |
| #ifdef CONFIG_OF |
| .of_match_table = ais328dq_id_table, |
| #endif |
| }, |
| .remove = ais328dq_i2c_remove, |
| .probe = ais328dq_i2c_probe, |
| .id_table = ais328dq_ids, |
| }; |
|
|
| module_i2c_driver(ais328dq_i2c_driver); |
|
|
| MODULE_DESCRIPTION("STMicroelectronics ais328dq i2c driver"); |
| MODULE_AUTHOR("Lorenzo Bianconi"); |
| MODULE_LICENSE("GPL v2"); |
|
|