1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO driver for the MiraMEMS DA280 3-axis accelerometer and
4 * IIO driver for the MiraMEMS DA226 2-axis accelerometer
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/acpi.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/byteorder/generic.h>
16 #define DA280_REG_CHIP_ID 0x01
17 #define DA280_REG_ACC_X_LSB 0x02
18 #define DA280_REG_ACC_Y_LSB 0x04
19 #define DA280_REG_ACC_Z_LSB 0x06
20 #define DA280_REG_MODE_BW 0x11
22 #define DA280_CHIP_ID 0x13
23 #define DA280_MODE_ENABLE 0x1e
24 #define DA280_MODE_DISABLE 0x9e
26 enum da280_chipset { da226, da280 };
29 * a value of + or -4096 corresponds to + or - 1G
30 * scale = 9.81 / 4096 = 0.002395019
33 static const int da280_nscale = 2395019;
35 #define DA280_CHANNEL(reg, axis) { \
39 .channel2 = IIO_MOD_##axis, \
40 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
41 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
44 static const struct iio_chan_spec da280_channels[] = {
45 DA280_CHANNEL(DA280_REG_ACC_X_LSB, X),
46 DA280_CHANNEL(DA280_REG_ACC_Y_LSB, Y),
47 DA280_CHANNEL(DA280_REG_ACC_Z_LSB, Z),
51 struct i2c_client *client;
54 static int da280_enable(struct i2c_client *client, bool enable)
56 u8 data = enable ? DA280_MODE_ENABLE : DA280_MODE_DISABLE;
58 return i2c_smbus_write_byte_data(client, DA280_REG_MODE_BW, data);
61 static int da280_read_raw(struct iio_dev *indio_dev,
62 struct iio_chan_spec const *chan,
63 int *val, int *val2, long mask)
65 struct da280_data *data = iio_priv(indio_dev);
69 case IIO_CHAN_INFO_RAW:
70 ret = i2c_smbus_read_word_data(data->client, chan->address);
74 * Values are 14 bits, stored as 16 bits with the 2
75 * least significant bits always 0.
77 *val = (short)ret >> 2;
79 case IIO_CHAN_INFO_SCALE:
82 return IIO_VAL_INT_PLUS_NANO;
88 static const struct iio_info da280_info = {
89 .read_raw = da280_read_raw,
92 static enum da280_chipset da280_match_acpi_device(struct device *dev)
94 const struct acpi_device_id *id;
96 id = acpi_match_device(dev->driver->acpi_match_table, dev);
100 return (enum da280_chipset) id->driver_data;
103 static int da280_probe(struct i2c_client *client,
104 const struct i2c_device_id *id)
107 struct iio_dev *indio_dev;
108 struct da280_data *data;
109 enum da280_chipset chip;
111 ret = i2c_smbus_read_byte_data(client, DA280_REG_CHIP_ID);
112 if (ret != DA280_CHIP_ID)
113 return (ret < 0) ? ret : -ENODEV;
115 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
119 data = iio_priv(indio_dev);
120 data->client = client;
121 i2c_set_clientdata(client, indio_dev);
123 indio_dev->info = &da280_info;
124 indio_dev->modes = INDIO_DIRECT_MODE;
125 indio_dev->channels = da280_channels;
127 if (ACPI_HANDLE(&client->dev)) {
128 chip = da280_match_acpi_device(&client->dev);
130 chip = id->driver_data;
134 indio_dev->name = "da226";
135 indio_dev->num_channels = 2;
137 indio_dev->name = "da280";
138 indio_dev->num_channels = 3;
141 ret = da280_enable(client, true);
145 ret = iio_device_register(indio_dev);
147 dev_err(&client->dev, "device_register failed\n");
148 da280_enable(client, false);
154 static int da280_remove(struct i2c_client *client)
156 struct iio_dev *indio_dev = i2c_get_clientdata(client);
158 iio_device_unregister(indio_dev);
160 return da280_enable(client, false);
163 #ifdef CONFIG_PM_SLEEP
164 static int da280_suspend(struct device *dev)
166 return da280_enable(to_i2c_client(dev), false);
169 static int da280_resume(struct device *dev)
171 return da280_enable(to_i2c_client(dev), true);
175 static SIMPLE_DEV_PM_OPS(da280_pm_ops, da280_suspend, da280_resume);
177 static const struct acpi_device_id da280_acpi_match[] = {
181 MODULE_DEVICE_TABLE(acpi, da280_acpi_match);
183 static const struct i2c_device_id da280_i2c_id[] = {
188 MODULE_DEVICE_TABLE(i2c, da280_i2c_id);
190 static struct i2c_driver da280_driver = {
193 .acpi_match_table = ACPI_PTR(da280_acpi_match),
196 .probe = da280_probe,
197 .remove = da280_remove,
198 .id_table = da280_i2c_id,
201 module_i2c_driver(da280_driver);
204 MODULE_DESCRIPTION("MiraMEMS DA280 3-Axis Accelerometer driver");
205 MODULE_LICENSE("GPL v2");