]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/iio/accel/stk8ba50.c
Merge tag 'tegra-for-4.3-cleanup' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-eoan-kernel.git] / drivers / iio / accel / stk8ba50.c
1 /**
2 * Sensortek STK8BA50 3-Axis Accelerometer
3 *
4 * Copyright (c) 2015, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * STK8BA50 7-bit I2C address: 0x18.
11 */
12
13 #include <linux/acpi.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19
20 #define STK8BA50_REG_XOUT 0x02
21 #define STK8BA50_REG_YOUT 0x04
22 #define STK8BA50_REG_ZOUT 0x06
23 #define STK8BA50_REG_RANGE 0x0F
24 #define STK8BA50_REG_POWMODE 0x11
25 #define STK8BA50_REG_SWRST 0x14
26
27 #define STK8BA50_MODE_NORMAL 0
28 #define STK8BA50_MODE_SUSPEND 1
29 #define STK8BA50_MODE_POWERBIT BIT(7)
30 #define STK8BA50_DATA_SHIFT 6
31 #define STK8BA50_RESET_CMD 0xB6
32
33 #define STK8BA50_DRIVER_NAME "stk8ba50"
34
35 #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069"
36
37 /*
38 * The accelerometer has four measurement ranges:
39 * +/-2g; +/-4g; +/-8g; +/-16g
40 *
41 * Acceleration values are 10-bit, 2's complement.
42 * Scales are calculated as following:
43 *
44 * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384
45 * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767
46 * etc.
47 *
48 * Scales are stored in this format:
49 * { <register value>, <scale value> }
50 *
51 * Locally, the range is stored as a table index.
52 */
53 static const int stk8ba50_scale_table[][2] = {
54 {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
55 };
56
57 struct stk8ba50_data {
58 struct i2c_client *client;
59 struct mutex lock;
60 int range;
61 };
62
63 #define STK8BA50_ACCEL_CHANNEL(reg, axis) { \
64 .type = IIO_ACCEL, \
65 .address = reg, \
66 .modified = 1, \
67 .channel2 = IIO_MOD_##axis, \
68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
69 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
70 }
71
72 static const struct iio_chan_spec stk8ba50_channels[] = {
73 STK8BA50_ACCEL_CHANNEL(STK8BA50_REG_XOUT, X),
74 STK8BA50_ACCEL_CHANNEL(STK8BA50_REG_YOUT, Y),
75 STK8BA50_ACCEL_CHANNEL(STK8BA50_REG_ZOUT, Z),
76 };
77
78 static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL);
79
80 static struct attribute *stk8ba50_attributes[] = {
81 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
82 NULL,
83 };
84
85 static const struct attribute_group stk8ba50_attribute_group = {
86 .attrs = stk8ba50_attributes
87 };
88
89 static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg)
90 {
91 int ret;
92 struct i2c_client *client = data->client;
93
94 ret = i2c_smbus_read_word_data(client, reg);
95 if (ret < 0) {
96 dev_err(&client->dev, "register read failed\n");
97 return ret;
98 }
99
100 return sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9);
101 }
102
103 static int stk8ba50_read_raw(struct iio_dev *indio_dev,
104 struct iio_chan_spec const *chan,
105 int *val, int *val2, long mask)
106 {
107 struct stk8ba50_data *data = iio_priv(indio_dev);
108
109 switch (mask) {
110 case IIO_CHAN_INFO_RAW:
111 mutex_lock(&data->lock);
112 *val = stk8ba50_read_accel(data, chan->address);
113 mutex_unlock(&data->lock);
114 return IIO_VAL_INT;
115 case IIO_CHAN_INFO_SCALE:
116 *val = 0;
117 *val2 = stk8ba50_scale_table[data->range][1];
118 return IIO_VAL_INT_PLUS_MICRO;
119 }
120
121 return -EINVAL;
122 }
123
124 static int stk8ba50_write_raw(struct iio_dev *indio_dev,
125 struct iio_chan_spec const *chan,
126 int val, int val2, long mask)
127 {
128 int ret;
129 int i;
130 int index = -1;
131 struct stk8ba50_data *data = iio_priv(indio_dev);
132
133 switch (mask) {
134 case IIO_CHAN_INFO_SCALE:
135 if (val != 0)
136 return -EINVAL;
137
138 for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++)
139 if (val2 == stk8ba50_scale_table[i][1]) {
140 index = i;
141 break;
142 }
143 if (index < 0)
144 return -EINVAL;
145
146 ret = i2c_smbus_write_byte_data(data->client,
147 STK8BA50_REG_RANGE,
148 stk8ba50_scale_table[index][0]);
149 if (ret < 0)
150 dev_err(&data->client->dev,
151 "failed to set measurement range\n");
152 else
153 data->range = index;
154
155 return ret;
156 }
157
158 return -EINVAL;
159 }
160
161 static const struct iio_info stk8ba50_info = {
162 .driver_module = THIS_MODULE,
163 .read_raw = stk8ba50_read_raw,
164 .write_raw = stk8ba50_write_raw,
165 .attrs = &stk8ba50_attribute_group,
166 };
167
168 static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode)
169 {
170 int ret;
171 u8 masked_reg;
172 struct i2c_client *client = data->client;
173
174 ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE);
175 if (ret < 0)
176 goto exit_err;
177
178 if (mode)
179 masked_reg = ret | STK8BA50_MODE_POWERBIT;
180 else
181 masked_reg = ret & (~STK8BA50_MODE_POWERBIT);
182
183 ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE,
184 masked_reg);
185 if (ret < 0)
186 goto exit_err;
187
188 return ret;
189
190 exit_err:
191 dev_err(&client->dev, "failed to change sensor mode\n");
192 return ret;
193 }
194
195 static int stk8ba50_probe(struct i2c_client *client,
196 const struct i2c_device_id *id)
197 {
198 int ret;
199 struct iio_dev *indio_dev;
200 struct stk8ba50_data *data;
201
202 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
203 if (!indio_dev) {
204 dev_err(&client->dev, "iio allocation failed!\n");
205 return -ENOMEM;
206 }
207
208 data = iio_priv(indio_dev);
209 data->client = client;
210 i2c_set_clientdata(client, indio_dev);
211 mutex_init(&data->lock);
212
213 indio_dev->dev.parent = &client->dev;
214 indio_dev->info = &stk8ba50_info;
215 indio_dev->name = STK8BA50_DRIVER_NAME;
216 indio_dev->modes = INDIO_DIRECT_MODE;
217 indio_dev->channels = stk8ba50_channels;
218 indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels);
219
220 /* Reset all registers on startup */
221 ret = i2c_smbus_write_byte_data(client,
222 STK8BA50_REG_SWRST, STK8BA50_RESET_CMD);
223 if (ret < 0) {
224 dev_err(&client->dev, "failed to reset sensor\n");
225 return ret;
226 }
227
228 /* The default range is +/-2g */
229 data->range = 0;
230
231 ret = iio_device_register(indio_dev);
232 if (ret < 0) {
233 dev_err(&client->dev, "device_register failed\n");
234 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
235 }
236
237 return ret;
238 }
239
240 static int stk8ba50_remove(struct i2c_client *client)
241 {
242 struct iio_dev *indio_dev = i2c_get_clientdata(client);
243
244 iio_device_unregister(indio_dev);
245
246 return stk8ba50_set_power(iio_priv(indio_dev), STK8BA50_MODE_SUSPEND);
247 }
248
249 #ifdef CONFIG_PM_SLEEP
250 static int stk8ba50_suspend(struct device *dev)
251 {
252 struct stk8ba50_data *data;
253
254 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
255
256 return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
257 }
258
259 static int stk8ba50_resume(struct device *dev)
260 {
261 struct stk8ba50_data *data;
262
263 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
264
265 return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
266 }
267
268 static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume);
269
270 #define STK8BA50_PM_OPS (&stk8ba50_pm_ops)
271 #else
272 #define STK8BA50_PM_OPS NULL
273 #endif
274
275 static const struct i2c_device_id stk8ba50_i2c_id[] = {
276 {"stk8ba50", 0},
277 {}
278 };
279
280 static const struct acpi_device_id stk8ba50_acpi_id[] = {
281 {"STK8BA50", 0},
282 {}
283 };
284
285 MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id);
286
287 static struct i2c_driver stk8ba50_driver = {
288 .driver = {
289 .name = "stk8ba50",
290 .pm = STK8BA50_PM_OPS,
291 .acpi_match_table = ACPI_PTR(stk8ba50_acpi_id),
292 },
293 .probe = stk8ba50_probe,
294 .remove = stk8ba50_remove,
295 .id_table = stk8ba50_i2c_id,
296 };
297
298 module_i2c_driver(stk8ba50_driver);
299
300 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
301 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
302 MODULE_LICENSE("GPL v2");