2 * Sensortek STK8312 3-Axis Accelerometer
4 * Copyright (c) 2015, Intel Corporation.
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.
10 * IIO driver for STK8312; 7-bit I2C address: 0x3D.
13 #include <linux/acpi.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define STK8312_REG_XOUT 0x00
22 #define STK8312_REG_YOUT 0x01
23 #define STK8312_REG_ZOUT 0x02
24 #define STK8312_REG_MODE 0x07
25 #define STK8312_REG_STH 0x13
26 #define STK8312_REG_RESET 0x20
27 #define STK8312_REG_AFECTRL 0x24
28 #define STK8312_REG_OTPADDR 0x3D
29 #define STK8312_REG_OTPDATA 0x3E
30 #define STK8312_REG_OTPCTRL 0x3F
32 #define STK8312_MODE_ACTIVE 1
33 #define STK8312_MODE_STANDBY 0
34 #define STK8312_MODE_MASK 0x01
35 #define STK8312_RNG_MASK 0xC0
36 #define STK8312_RNG_SHIFT 6
37 #define STK8312_READ_RETRIES 16
39 #define STK8312_DRIVER_NAME "stk8312"
42 * The accelerometer has two measurement ranges:
44 * -6g - +6g (8-bit, signed)
45 * -16g - +16g (8-bit, signed)
47 * scale1 = (6 + 6) * 9.81 / (2^8 - 1) = 0.4616
48 * scale2 = (16 + 16) * 9.81 / (2^8 - 1) = 1.2311
50 #define STK8312_SCALE_AVAIL "0.4616 1.2311"
52 static const int stk8312_scale_table[][2] = {
53 {0, 461600}, {1, 231100}
56 #define STK8312_ACCEL_CHANNEL(reg, axis) { \
60 .channel2 = IIO_MOD_##axis, \
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
65 static const struct iio_chan_spec stk8312_channels[] = {
66 STK8312_ACCEL_CHANNEL(STK8312_REG_XOUT, X),
67 STK8312_ACCEL_CHANNEL(STK8312_REG_YOUT, Y),
68 STK8312_ACCEL_CHANNEL(STK8312_REG_ZOUT, Z),
72 struct i2c_client *client;
78 static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL);
80 static struct attribute *stk8312_attributes[] = {
81 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
85 static const struct attribute_group stk8312_attribute_group = {
86 .attrs = stk8312_attributes
89 static int stk8312_otp_init(struct stk8312_data *data)
93 struct i2c_client *client = data->client;
95 ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70);
98 ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02);
103 usleep_range(1000, 5000);
104 ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL);
108 } while (!(ret & 0x80) && count > 0);
113 ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA);
117 ret = i2c_smbus_write_byte_data(data->client,
118 STK8312_REG_AFECTRL, ret);
126 dev_err(&client->dev, "failed to initialize sensor\n");
130 static int stk8312_set_mode(struct stk8312_data *data, u8 mode)
134 struct i2c_client *client = data->client;
138 else if (mode == data->mode)
141 ret = i2c_smbus_read_byte_data(client, STK8312_REG_MODE);
143 dev_err(&client->dev, "failed to change sensor mode\n");
146 masked_reg = ret & (~STK8312_MODE_MASK);
149 ret = i2c_smbus_write_byte_data(client,
150 STK8312_REG_MODE, masked_reg);
152 dev_err(&client->dev, "failed to change sensor mode\n");
157 if (mode == STK8312_MODE_ACTIVE) {
158 /* Need to run OTP sequence before entering active mode */
159 usleep_range(1000, 5000);
160 ret = stk8312_otp_init(data);
166 static int stk8312_set_range(struct stk8312_data *data, u8 range)
171 struct i2c_client *client = data->client;
173 if (range != 1 && range != 2)
175 else if (range == data->range)
179 /* We need to go in standby mode to modify registers */
180 ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
184 ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH);
186 dev_err(&client->dev, "failed to change sensor range\n");
190 masked_reg = ret & (~STK8312_RNG_MASK);
191 masked_reg |= range << STK8312_RNG_SHIFT;
193 ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg);
195 dev_err(&client->dev, "failed to change sensor range\n");
199 return stk8312_set_mode(data, mode);
202 static int stk8312_read_accel(struct stk8312_data *data, u8 address)
205 struct i2c_client *client = data->client;
210 ret = i2c_smbus_read_byte_data(client, address);
212 dev_err(&client->dev, "register read failed\n");
216 return sign_extend32(ret, 7);
219 static int stk8312_read_raw(struct iio_dev *indio_dev,
220 struct iio_chan_spec const *chan,
221 int *val, int *val2, long mask)
223 struct stk8312_data *data = iio_priv(indio_dev);
225 if (chan->type != IIO_ACCEL)
229 case IIO_CHAN_INFO_RAW:
230 mutex_lock(&data->lock);
231 *val = stk8312_read_accel(data, chan->address);
232 mutex_unlock(&data->lock);
234 case IIO_CHAN_INFO_SCALE:
235 *val = stk8312_scale_table[data->range - 1][0];
236 *val2 = stk8312_scale_table[data->range - 1][1];
237 return IIO_VAL_INT_PLUS_MICRO;
243 static int stk8312_write_raw(struct iio_dev *indio_dev,
244 struct iio_chan_spec const *chan,
245 int val, int val2, long mask)
250 struct stk8312_data *data = iio_priv(indio_dev);
253 case IIO_CHAN_INFO_SCALE:
254 for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++)
255 if (val == stk8312_scale_table[i][0] &&
256 val2 == stk8312_scale_table[i][1]) {
263 mutex_lock(&data->lock);
264 ret = stk8312_set_range(data, index);
265 mutex_unlock(&data->lock);
273 static const struct iio_info stk8312_info = {
274 .driver_module = THIS_MODULE,
275 .read_raw = stk8312_read_raw,
276 .write_raw = stk8312_write_raw,
277 .attrs = &stk8312_attribute_group,
280 static int stk8312_probe(struct i2c_client *client,
281 const struct i2c_device_id *id)
284 struct iio_dev *indio_dev;
285 struct stk8312_data *data;
287 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
289 dev_err(&client->dev, "iio allocation failed!\n");
293 data = iio_priv(indio_dev);
294 data->client = client;
295 i2c_set_clientdata(client, indio_dev);
296 mutex_init(&data->lock);
298 indio_dev->dev.parent = &client->dev;
299 indio_dev->info = &stk8312_info;
300 indio_dev->name = STK8312_DRIVER_NAME;
301 indio_dev->modes = INDIO_DIRECT_MODE;
302 indio_dev->channels = stk8312_channels;
303 indio_dev->num_channels = ARRAY_SIZE(stk8312_channels);
305 /* A software reset is recommended at power-on */
306 ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00);
308 dev_err(&client->dev, "failed to reset sensor\n");
311 ret = stk8312_set_range(data, 1);
315 ret = stk8312_set_mode(data, STK8312_MODE_ACTIVE);
319 ret = iio_device_register(indio_dev);
321 dev_err(&client->dev, "device_register failed\n");
322 stk8312_set_mode(data, STK8312_MODE_STANDBY);
328 static int stk8312_remove(struct i2c_client *client)
330 struct iio_dev *indio_dev = i2c_get_clientdata(client);
332 iio_device_unregister(indio_dev);
334 return stk8312_set_mode(iio_priv(indio_dev), STK8312_MODE_STANDBY);
337 #ifdef CONFIG_PM_SLEEP
338 static int stk8312_suspend(struct device *dev)
340 struct stk8312_data *data;
342 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
344 return stk8312_set_mode(data, STK8312_MODE_STANDBY);
347 static int stk8312_resume(struct device *dev)
349 struct stk8312_data *data;
351 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
353 return stk8312_set_mode(data, STK8312_MODE_ACTIVE);
356 static SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend, stk8312_resume);
358 #define STK8312_PM_OPS (&stk8312_pm_ops)
360 #define STK8312_PM_OPS NULL
363 static const struct i2c_device_id stk8312_i2c_id[] = {
368 static const struct acpi_device_id stk8312_acpi_id[] = {
373 MODULE_DEVICE_TABLE(acpi, stk8312_acpi_id);
375 static struct i2c_driver stk8312_driver = {
378 .pm = STK8312_PM_OPS,
379 .acpi_match_table = ACPI_PTR(stk8312_acpi_id),
381 .probe = stk8312_probe,
382 .remove = stk8312_remove,
383 .id_table = stk8312_i2c_id,
386 module_i2c_driver(stk8312_driver);
388 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
389 MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver");
390 MODULE_LICENSE("GPL v2");