2 * Sensortek STK8BA50 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 * STK8BA50 7-bit I2C address: 0x18.
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>
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
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
33 #define STK8BA50_DRIVER_NAME "stk8ba50"
35 #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069"
38 * The accelerometer has four measurement ranges:
39 * +/-2g; +/-4g; +/-8g; +/-16g
41 * Acceleration values are 10-bit, 2's complement.
42 * Scales are calculated as following:
44 * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384
45 * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767
48 * Scales are stored in this format:
49 * { <register value>, <scale value> }
51 * Locally, the range is stored as a table index.
53 static const int stk8ba50_scale_table
[][2] = {
54 {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
57 struct stk8ba50_data
{
58 struct i2c_client
*client
;
63 #define STK8BA50_ACCEL_CHANNEL(reg, axis) { \
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), \
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
),
78 static IIO_CONST_ATTR(in_accel_scale_available
, STK8BA50_SCALE_AVAIL
);
80 static struct attribute
*stk8ba50_attributes
[] = {
81 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
85 static const struct attribute_group stk8ba50_attribute_group
= {
86 .attrs
= stk8ba50_attributes
89 static int stk8ba50_read_accel(struct stk8ba50_data
*data
, u8 reg
)
92 struct i2c_client
*client
= data
->client
;
94 ret
= i2c_smbus_read_word_data(client
, reg
);
96 dev_err(&client
->dev
, "register read failed\n");
100 return sign_extend32(ret
>> STK8BA50_DATA_SHIFT
, 9);
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
)
107 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
110 case IIO_CHAN_INFO_RAW
:
111 mutex_lock(&data
->lock
);
112 *val
= stk8ba50_read_accel(data
, chan
->address
);
113 mutex_unlock(&data
->lock
);
115 case IIO_CHAN_INFO_SCALE
:
117 *val2
= stk8ba50_scale_table
[data
->range
][1];
118 return IIO_VAL_INT_PLUS_MICRO
;
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
)
131 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
134 case IIO_CHAN_INFO_SCALE
:
138 for (i
= 0; i
< ARRAY_SIZE(stk8ba50_scale_table
); i
++)
139 if (val2
== stk8ba50_scale_table
[i
][1]) {
146 ret
= i2c_smbus_write_byte_data(data
->client
,
148 stk8ba50_scale_table
[index
][0]);
150 dev_err(&data
->client
->dev
,
151 "failed to set measurement range\n");
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
,
168 static int stk8ba50_set_power(struct stk8ba50_data
*data
, bool mode
)
172 struct i2c_client
*client
= data
->client
;
174 ret
= i2c_smbus_read_byte_data(client
, STK8BA50_REG_POWMODE
);
179 masked_reg
= ret
| STK8BA50_MODE_POWERBIT
;
181 masked_reg
= ret
& (~STK8BA50_MODE_POWERBIT
);
183 ret
= i2c_smbus_write_byte_data(client
, STK8BA50_REG_POWMODE
,
191 dev_err(&client
->dev
, "failed to change sensor mode\n");
195 static int stk8ba50_probe(struct i2c_client
*client
,
196 const struct i2c_device_id
*id
)
199 struct iio_dev
*indio_dev
;
200 struct stk8ba50_data
*data
;
202 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
204 dev_err(&client
->dev
, "iio allocation failed!\n");
208 data
= iio_priv(indio_dev
);
209 data
->client
= client
;
210 i2c_set_clientdata(client
, indio_dev
);
211 mutex_init(&data
->lock
);
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
);
220 /* Reset all registers on startup */
221 ret
= i2c_smbus_write_byte_data(client
,
222 STK8BA50_REG_SWRST
, STK8BA50_RESET_CMD
);
224 dev_err(&client
->dev
, "failed to reset sensor\n");
228 /* The default range is +/-2g */
231 ret
= iio_device_register(indio_dev
);
233 dev_err(&client
->dev
, "device_register failed\n");
234 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
240 static int stk8ba50_remove(struct i2c_client
*client
)
242 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
244 iio_device_unregister(indio_dev
);
246 return stk8ba50_set_power(iio_priv(indio_dev
), STK8BA50_MODE_SUSPEND
);
249 #ifdef CONFIG_PM_SLEEP
250 static int stk8ba50_suspend(struct device
*dev
)
252 struct stk8ba50_data
*data
;
254 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
256 return stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
259 static int stk8ba50_resume(struct device
*dev
)
261 struct stk8ba50_data
*data
;
263 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
265 return stk8ba50_set_power(data
, STK8BA50_MODE_NORMAL
);
268 static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops
, stk8ba50_suspend
, stk8ba50_resume
);
270 #define STK8BA50_PM_OPS (&stk8ba50_pm_ops)
272 #define STK8BA50_PM_OPS NULL
275 static const struct i2c_device_id stk8ba50_i2c_id
[] = {
280 static const struct acpi_device_id stk8ba50_acpi_id
[] = {
285 MODULE_DEVICE_TABLE(acpi
, stk8ba50_acpi_id
);
287 static struct i2c_driver stk8ba50_driver
= {
290 .pm
= STK8BA50_PM_OPS
,
291 .acpi_match_table
= ACPI_PTR(stk8ba50_acpi_id
),
293 .probe
= stk8ba50_probe
,
294 .remove
= stk8ba50_remove
,
295 .id_table
= stk8ba50_i2c_id
,
298 module_i2c_driver(stk8ba50_driver
);
300 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
301 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
302 MODULE_LICENSE("GPL v2");