xref: /openbmc/linux/drivers/iio/accel/stk8312.c (revision bbde9fc1824aab58bc78c084163007dd6c03fe5b)
1 /**
2  * Sensortek STK8312 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  * IIO driver for STK8312; 7-bit I2C address: 0x3D.
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/delay.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 
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
31 
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
38 
39 #define STK8312_DRIVER_NAME		"stk8312"
40 
41 /*
42  * The accelerometer has two measurement ranges:
43  *
44  * -6g - +6g (8-bit, signed)
45  * -16g - +16g (8-bit, signed)
46  *
47  * scale1 = (6 + 6) * 9.81 / (2^8 - 1)     = 0.4616
48  * scale2 = (16 + 16) * 9.81 / (2^8 - 1)   = 1.2311
49  */
50 #define STK8312_SCALE_AVAIL		"0.4616 1.2311"
51 
52 static const int stk8312_scale_table[][2] = {
53 	{0, 461600}, {1, 231100}
54 };
55 
56 #define STK8312_ACCEL_CHANNEL(reg, axis) {			\
57 	.type = IIO_ACCEL,					\
58 	.address = reg,						\
59 	.modified = 1,						\
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),	\
63 }
64 
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),
69 };
70 
71 struct stk8312_data {
72 	struct i2c_client *client;
73 	struct mutex lock;
74 	int range;
75 	u8 mode;
76 };
77 
78 static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL);
79 
80 static struct attribute *stk8312_attributes[] = {
81 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
82 	NULL,
83 };
84 
85 static const struct attribute_group stk8312_attribute_group = {
86 	.attrs = stk8312_attributes
87 };
88 
89 static int stk8312_otp_init(struct stk8312_data *data)
90 {
91 	int ret;
92 	int count = 10;
93 	struct i2c_client *client = data->client;
94 
95 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70);
96 	if (ret < 0)
97 		goto exit_err;
98 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02);
99 	if (ret < 0)
100 		goto exit_err;
101 
102 	do {
103 		usleep_range(1000, 5000);
104 		ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL);
105 		if (ret < 0)
106 			goto exit_err;
107 		count--;
108 	} while (!(ret & 0x80) && count > 0);
109 
110 	if (count == 0)
111 		goto exit_err;
112 
113 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA);
114 	if (ret < 0)
115 		goto exit_err;
116 
117 	ret = i2c_smbus_write_byte_data(data->client,
118 			STK8312_REG_AFECTRL, ret);
119 	if (ret < 0)
120 		goto exit_err;
121 	msleep(150);
122 
123 	return ret;
124 
125 exit_err:
126 	dev_err(&client->dev, "failed to initialize sensor\n");
127 	return ret;
128 }
129 
130 static int stk8312_set_mode(struct stk8312_data *data, u8 mode)
131 {
132 	int ret;
133 	u8 masked_reg;
134 	struct i2c_client *client = data->client;
135 
136 	if (mode > 1)
137 		return -EINVAL;
138 	else if (mode == data->mode)
139 		return 0;
140 
141 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_MODE);
142 	if (ret < 0) {
143 		dev_err(&client->dev, "failed to change sensor mode\n");
144 		return ret;
145 	}
146 	masked_reg = ret & (~STK8312_MODE_MASK);
147 	masked_reg |= mode;
148 
149 	ret = i2c_smbus_write_byte_data(client,
150 			STK8312_REG_MODE, masked_reg);
151 	if (ret < 0) {
152 		dev_err(&client->dev, "failed to change sensor mode\n");
153 		return ret;
154 	}
155 
156 	data->mode = mode;
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);
161 	}
162 
163 	return ret;
164 }
165 
166 static int stk8312_set_range(struct stk8312_data *data, u8 range)
167 {
168 	int ret;
169 	u8 masked_reg;
170 	u8 mode;
171 	struct i2c_client *client = data->client;
172 
173 	if (range != 1 && range != 2)
174 		return -EINVAL;
175 	else if (range == data->range)
176 		return 0;
177 
178 	mode = data->mode;
179 	/* We need to go in standby mode to modify registers */
180 	ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
181 	if (ret < 0)
182 		return ret;
183 
184 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH);
185 	if (ret < 0) {
186 		dev_err(&client->dev, "failed to change sensor range\n");
187 		return ret;
188 	}
189 
190 	masked_reg = ret & (~STK8312_RNG_MASK);
191 	masked_reg |= range << STK8312_RNG_SHIFT;
192 
193 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg);
194 	if (ret < 0)
195 		dev_err(&client->dev, "failed to change sensor range\n");
196 	else
197 		data->range = range;
198 
199 	return stk8312_set_mode(data, mode);
200 }
201 
202 static int stk8312_read_accel(struct stk8312_data *data, u8 address)
203 {
204 	int ret;
205 	struct i2c_client *client = data->client;
206 
207 	if (address > 2)
208 		return -EINVAL;
209 
210 	ret = i2c_smbus_read_byte_data(client, address);
211 	if (ret < 0) {
212 		dev_err(&client->dev, "register read failed\n");
213 		return ret;
214 	}
215 
216 	return sign_extend32(ret, 7);
217 }
218 
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)
222 {
223 	struct stk8312_data *data = iio_priv(indio_dev);
224 
225 	if (chan->type != IIO_ACCEL)
226 		return -EINVAL;
227 
228 	switch (mask) {
229 	case IIO_CHAN_INFO_RAW:
230 		mutex_lock(&data->lock);
231 		*val = stk8312_read_accel(data, chan->address);
232 		mutex_unlock(&data->lock);
233 		return IIO_VAL_INT;
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;
238 	}
239 
240 	return -EINVAL;
241 }
242 
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)
246 {
247 	int i;
248 	int index = -1;
249 	int ret;
250 	struct stk8312_data *data = iio_priv(indio_dev);
251 
252 	switch (mask) {
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]) {
257 				index = i + 1;
258 				break;
259 			}
260 		if (index < 0)
261 			return -EINVAL;
262 
263 		mutex_lock(&data->lock);
264 		ret = stk8312_set_range(data, index);
265 		mutex_unlock(&data->lock);
266 
267 		return ret;
268 	}
269 
270 	return -EINVAL;
271 }
272 
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,
278 };
279 
280 static int stk8312_probe(struct i2c_client *client,
281 			 const struct i2c_device_id *id)
282 {
283 	int ret;
284 	struct iio_dev *indio_dev;
285 	struct stk8312_data *data;
286 
287 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
288 	if (!indio_dev) {
289 		dev_err(&client->dev, "iio allocation failed!\n");
290 		return -ENOMEM;
291 	}
292 
293 	data = iio_priv(indio_dev);
294 	data->client = client;
295 	i2c_set_clientdata(client, indio_dev);
296 	mutex_init(&data->lock);
297 
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);
304 
305 	/* A software reset is recommended at power-on */
306 	ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00);
307 	if (ret < 0) {
308 		dev_err(&client->dev, "failed to reset sensor\n");
309 		return ret;
310 	}
311 	ret = stk8312_set_range(data, 1);
312 	if (ret < 0)
313 		return ret;
314 
315 	ret = stk8312_set_mode(data, STK8312_MODE_ACTIVE);
316 	if (ret < 0)
317 		return ret;
318 
319 	ret = iio_device_register(indio_dev);
320 	if (ret < 0) {
321 		dev_err(&client->dev, "device_register failed\n");
322 		stk8312_set_mode(data, STK8312_MODE_STANDBY);
323 	}
324 
325 	return ret;
326 }
327 
328 static int stk8312_remove(struct i2c_client *client)
329 {
330 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
331 
332 	iio_device_unregister(indio_dev);
333 
334 	return stk8312_set_mode(iio_priv(indio_dev), STK8312_MODE_STANDBY);
335 }
336 
337 #ifdef CONFIG_PM_SLEEP
338 static int stk8312_suspend(struct device *dev)
339 {
340 	struct stk8312_data *data;
341 
342 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
343 
344 	return stk8312_set_mode(data, STK8312_MODE_STANDBY);
345 }
346 
347 static int stk8312_resume(struct device *dev)
348 {
349 	struct stk8312_data *data;
350 
351 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
352 
353 	return stk8312_set_mode(data, STK8312_MODE_ACTIVE);
354 }
355 
356 static SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend, stk8312_resume);
357 
358 #define STK8312_PM_OPS (&stk8312_pm_ops)
359 #else
360 #define STK8312_PM_OPS NULL
361 #endif
362 
363 static const struct i2c_device_id stk8312_i2c_id[] = {
364 	{"STK8312", 0},
365 	{}
366 };
367 
368 static const struct acpi_device_id stk8312_acpi_id[] = {
369 	{"STK8312", 0},
370 	{}
371 };
372 
373 MODULE_DEVICE_TABLE(acpi, stk8312_acpi_id);
374 
375 static struct i2c_driver stk8312_driver = {
376 	.driver = {
377 		.name = "stk8312",
378 		.pm = STK8312_PM_OPS,
379 		.acpi_match_table = ACPI_PTR(stk8312_acpi_id),
380 	},
381 	.probe =            stk8312_probe,
382 	.remove =           stk8312_remove,
383 	.id_table =         stk8312_i2c_id,
384 };
385 
386 module_i2c_driver(stk8312_driver);
387 
388 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
389 MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver");
390 MODULE_LICENSE("GPL v2");
391