sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / accel / da280.c
blobed8343aeac9c92d17e07ab12475f6a1678087e5e
1 /**
2 * IIO driver for the MiraMEMS DA280 3-axis accelerometer and
3 * IIO driver for the MiraMEMS DA226 2-axis accelerometer
5 * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/byteorder/generic.h>
18 #define DA280_REG_CHIP_ID 0x01
19 #define DA280_REG_ACC_X_LSB 0x02
20 #define DA280_REG_ACC_Y_LSB 0x04
21 #define DA280_REG_ACC_Z_LSB 0x06
22 #define DA280_REG_MODE_BW 0x11
24 #define DA280_CHIP_ID 0x13
25 #define DA280_MODE_ENABLE 0x1e
26 #define DA280_MODE_DISABLE 0x9e
28 enum { da226, da280 };
31 * a value of + or -4096 corresponds to + or - 1G
32 * scale = 9.81 / 4096 = 0.002395019
35 static const int da280_nscale = 2395019;
37 #define DA280_CHANNEL(reg, axis) { \
38 .type = IIO_ACCEL, \
39 .address = reg, \
40 .modified = 1, \
41 .channel2 = IIO_MOD_##axis, \
42 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
43 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
46 static const struct iio_chan_spec da280_channels[] = {
47 DA280_CHANNEL(DA280_REG_ACC_X_LSB, X),
48 DA280_CHANNEL(DA280_REG_ACC_Y_LSB, Y),
49 DA280_CHANNEL(DA280_REG_ACC_Z_LSB, Z),
52 struct da280_data {
53 struct i2c_client *client;
56 static int da280_enable(struct i2c_client *client, bool enable)
58 u8 data = enable ? DA280_MODE_ENABLE : DA280_MODE_DISABLE;
60 return i2c_smbus_write_byte_data(client, DA280_REG_MODE_BW, data);
63 static int da280_read_raw(struct iio_dev *indio_dev,
64 struct iio_chan_spec const *chan,
65 int *val, int *val2, long mask)
67 struct da280_data *data = iio_priv(indio_dev);
68 int ret;
70 switch (mask) {
71 case IIO_CHAN_INFO_RAW:
72 ret = i2c_smbus_read_word_data(data->client, chan->address);
73 if (ret < 0)
74 return ret;
76 * Values are 14 bits, stored as 16 bits with the 2
77 * least significant bits always 0.
79 *val = (short)ret >> 2;
80 return IIO_VAL_INT;
81 case IIO_CHAN_INFO_SCALE:
82 *val = 0;
83 *val2 = da280_nscale;
84 return IIO_VAL_INT_PLUS_NANO;
85 default:
86 return -EINVAL;
90 static const struct iio_info da280_info = {
91 .driver_module = THIS_MODULE,
92 .read_raw = da280_read_raw,
95 static int da280_probe(struct i2c_client *client,
96 const struct i2c_device_id *id)
98 int ret;
99 struct iio_dev *indio_dev;
100 struct da280_data *data;
102 ret = i2c_smbus_read_byte_data(client, DA280_REG_CHIP_ID);
103 if (ret != DA280_CHIP_ID)
104 return (ret < 0) ? ret : -ENODEV;
106 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
107 if (!indio_dev)
108 return -ENOMEM;
110 data = iio_priv(indio_dev);
111 data->client = client;
112 i2c_set_clientdata(client, indio_dev);
114 indio_dev->dev.parent = &client->dev;
115 indio_dev->info = &da280_info;
116 indio_dev->modes = INDIO_DIRECT_MODE;
117 indio_dev->channels = da280_channels;
118 if (id->driver_data == da226) {
119 indio_dev->name = "da226";
120 indio_dev->num_channels = 2;
121 } else {
122 indio_dev->name = "da280";
123 indio_dev->num_channels = 3;
126 ret = da280_enable(client, true);
127 if (ret < 0)
128 return ret;
130 ret = iio_device_register(indio_dev);
131 if (ret < 0) {
132 dev_err(&client->dev, "device_register failed\n");
133 da280_enable(client, false);
136 return ret;
139 static int da280_remove(struct i2c_client *client)
141 struct iio_dev *indio_dev = i2c_get_clientdata(client);
143 iio_device_unregister(indio_dev);
145 return da280_enable(client, false);
148 #ifdef CONFIG_PM_SLEEP
149 static int da280_suspend(struct device *dev)
151 return da280_enable(to_i2c_client(dev), false);
154 static int da280_resume(struct device *dev)
156 return da280_enable(to_i2c_client(dev), true);
158 #endif
160 static SIMPLE_DEV_PM_OPS(da280_pm_ops, da280_suspend, da280_resume);
162 static const struct i2c_device_id da280_i2c_id[] = {
163 { "da226", da226 },
164 { "da280", da280 },
167 MODULE_DEVICE_TABLE(i2c, da280_i2c_id);
169 static struct i2c_driver da280_driver = {
170 .driver = {
171 .name = "da280",
172 .pm = &da280_pm_ops,
174 .probe = da280_probe,
175 .remove = da280_remove,
176 .id_table = da280_i2c_id,
179 module_i2c_driver(da280_driver);
181 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
182 MODULE_DESCRIPTION("MiraMEMS DA280 3-Axis Accelerometer driver");
183 MODULE_LICENSE("GPL v2");