xref: /linux/drivers/iio/accel/stk8312.c (revision da5b2ad1c2f18834cb1ce429e2e5a5cf5cbdf21b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sensortek STK8312 3-Axis Accelerometer
4  *
5  * Copyright (c) 2015, Intel Corporation.
6  *
7  * IIO driver for STK8312; 7-bit I2C address: 0x3D.
8  */
9 
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/trigger.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
21 
22 #define STK8312_REG_XOUT		0x00
23 #define STK8312_REG_YOUT		0x01
24 #define STK8312_REG_ZOUT		0x02
25 #define STK8312_REG_INTSU		0x06
26 #define STK8312_REG_MODE		0x07
27 #define STK8312_REG_SR			0x08
28 #define STK8312_REG_STH			0x13
29 #define STK8312_REG_RESET		0x20
30 #define STK8312_REG_AFECTRL		0x24
31 #define STK8312_REG_OTPADDR		0x3D
32 #define STK8312_REG_OTPDATA		0x3E
33 #define STK8312_REG_OTPCTRL		0x3F
34 
35 #define STK8312_MODE_ACTIVE		BIT(0)
36 #define STK8312_MODE_STANDBY		0x00
37 #define STK8312_MODE_INT_AH_PP		0xC0	/* active-high, push-pull */
38 #define STK8312_DREADY_BIT		BIT(4)
39 #define STK8312_RNG_6G			1
40 #define STK8312_RNG_SHIFT		6
41 #define STK8312_RNG_MASK		GENMASK(7, 6)
42 #define STK8312_SR_MASK			GENMASK(2, 0)
43 #define STK8312_SR_400HZ_IDX		0
44 #define STK8312_ALL_CHANNEL_MASK	GENMASK(2, 0)
45 #define STK8312_ALL_CHANNEL_SIZE	3
46 
47 #define STK8312_DRIVER_NAME		"stk8312"
48 #define STK8312_IRQ_NAME		"stk8312_event"
49 
50 /*
51  * The accelerometer has two measurement ranges:
52  *
53  * -6g - +6g (8-bit, signed)
54  * -16g - +16g (8-bit, signed)
55  *
56  * scale1 = (6 + 6) * 9.81 / (2^8 - 1)     = 0.4616
57  * scale2 = (16 + 16) * 9.81 / (2^8 - 1)   = 1.2311
58  */
59 #define STK8312_SCALE_AVAIL		"0.4616 1.2311"
60 
61 static const int stk8312_scale_table[][2] = {
62 	{0, 461600}, {1, 231100}
63 };
64 
65 static const struct {
66 	int val;
67 	int val2;
68 } stk8312_samp_freq_table[] = {
69 	{400, 0}, {200, 0}, {100, 0}, {50, 0}, {25, 0},
70 	{12, 500000}, {6, 250000}, {3, 125000}
71 };
72 
73 #define STK8312_ACCEL_CHANNEL(index, reg, axis) {			\
74 	.type = IIO_ACCEL,						\
75 	.address = reg,							\
76 	.modified = 1,							\
77 	.channel2 = IIO_MOD_##axis,					\
78 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
79 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
80 				    BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
81 	.scan_index = index,						\
82 	.scan_type = {							\
83 		.sign = 's',						\
84 		.realbits = 8,						\
85 		.storagebits = 8,					\
86 		.endianness = IIO_CPU,					\
87 	},								\
88 }
89 
90 static const struct iio_chan_spec stk8312_channels[] = {
91 	STK8312_ACCEL_CHANNEL(0, STK8312_REG_XOUT, X),
92 	STK8312_ACCEL_CHANNEL(1, STK8312_REG_YOUT, Y),
93 	STK8312_ACCEL_CHANNEL(2, STK8312_REG_ZOUT, Z),
94 	IIO_CHAN_SOFT_TIMESTAMP(3),
95 };
96 
97 struct stk8312_data {
98 	struct i2c_client *client;
99 	struct mutex lock;
100 	u8 range;
101 	u8 sample_rate_idx;
102 	u8 mode;
103 	struct iio_trigger *dready_trig;
104 	bool dready_trigger_on;
105 	/* Ensure timestamp is naturally aligned */
106 	struct {
107 		s8 chans[3];
108 		s64 timestamp __aligned(8);
109 	} scan;
110 };
111 
112 static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL);
113 
114 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400");
115 
116 static struct attribute *stk8312_attributes[] = {
117 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
118 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
119 	NULL,
120 };
121 
122 static const struct attribute_group stk8312_attribute_group = {
123 	.attrs = stk8312_attributes
124 };
125 
126 static int stk8312_otp_init(struct stk8312_data *data)
127 {
128 	int ret;
129 	int count = 10;
130 	struct i2c_client *client = data->client;
131 
132 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70);
133 	if (ret < 0)
134 		goto exit_err;
135 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02);
136 	if (ret < 0)
137 		goto exit_err;
138 
139 	do {
140 		usleep_range(1000, 5000);
141 		ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL);
142 		if (ret < 0)
143 			goto exit_err;
144 		count--;
145 	} while (!(ret & BIT(7)) && count > 0);
146 
147 	if (count == 0) {
148 		ret = -ETIMEDOUT;
149 		goto exit_err;
150 	}
151 
152 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA);
153 	if (ret == 0)
154 		ret = -EINVAL;
155 	if (ret < 0)
156 		goto exit_err;
157 
158 	ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_AFECTRL, ret);
159 	if (ret < 0)
160 		goto exit_err;
161 	msleep(150);
162 
163 	return 0;
164 
165 exit_err:
166 	dev_err(&client->dev, "failed to initialize sensor\n");
167 	return ret;
168 }
169 
170 static int stk8312_set_mode(struct stk8312_data *data, u8 mode)
171 {
172 	int ret;
173 	struct i2c_client *client = data->client;
174 
175 	if (mode == data->mode)
176 		return 0;
177 
178 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_MODE, mode);
179 	if (ret < 0) {
180 		dev_err(&client->dev, "failed to change sensor mode\n");
181 		return ret;
182 	}
183 
184 	data->mode = mode;
185 	if (mode & STK8312_MODE_ACTIVE) {
186 		/* Need to run OTP sequence before entering active mode */
187 		usleep_range(1000, 5000);
188 		ret = stk8312_otp_init(data);
189 	}
190 
191 	return ret;
192 }
193 
194 static int stk8312_set_interrupts(struct stk8312_data *data, u8 int_mask)
195 {
196 	int ret;
197 	u8 mode;
198 	struct i2c_client *client = data->client;
199 
200 	mode = data->mode;
201 	/* We need to go in standby mode to modify registers */
202 	ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
203 	if (ret < 0)
204 		return ret;
205 
206 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_INTSU, int_mask);
207 	if (ret < 0) {
208 		dev_err(&client->dev, "failed to set interrupts\n");
209 		stk8312_set_mode(data, mode);
210 		return ret;
211 	}
212 
213 	return stk8312_set_mode(data, mode);
214 }
215 
216 static int stk8312_data_rdy_trigger_set_state(struct iio_trigger *trig,
217 					      bool state)
218 {
219 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
220 	struct stk8312_data *data = iio_priv(indio_dev);
221 	int ret;
222 
223 	if (state)
224 		ret = stk8312_set_interrupts(data, STK8312_DREADY_BIT);
225 	else
226 		ret = stk8312_set_interrupts(data, 0x00);
227 
228 	if (ret < 0) {
229 		dev_err(&data->client->dev, "failed to set trigger state\n");
230 		return ret;
231 	}
232 
233 	data->dready_trigger_on = state;
234 
235 	return 0;
236 }
237 
238 static const struct iio_trigger_ops stk8312_trigger_ops = {
239 	.set_trigger_state = stk8312_data_rdy_trigger_set_state,
240 };
241 
242 static int stk8312_set_sample_rate(struct stk8312_data *data, u8 rate)
243 {
244 	int ret;
245 	u8 masked_reg;
246 	u8 mode;
247 	struct i2c_client *client = data->client;
248 
249 	if (rate == data->sample_rate_idx)
250 		return 0;
251 
252 	mode = data->mode;
253 	/* We need to go in standby mode to modify registers */
254 	ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
255 	if (ret < 0)
256 		return ret;
257 
258 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_SR);
259 	if (ret < 0)
260 		goto err_activate;
261 
262 	masked_reg = (ret & (~STK8312_SR_MASK)) | rate;
263 
264 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_SR, masked_reg);
265 	if (ret < 0)
266 		goto err_activate;
267 
268 	data->sample_rate_idx = rate;
269 
270 	return stk8312_set_mode(data, mode);
271 
272 err_activate:
273 	dev_err(&client->dev, "failed to set sampling rate\n");
274 	stk8312_set_mode(data, mode);
275 
276 	return ret;
277 }
278 
279 static int stk8312_set_range(struct stk8312_data *data, u8 range)
280 {
281 	int ret;
282 	u8 masked_reg;
283 	u8 mode;
284 	struct i2c_client *client = data->client;
285 
286 	if (range != 1 && range != 2)
287 		return -EINVAL;
288 	else if (range == data->range)
289 		return 0;
290 
291 	mode = data->mode;
292 	/* We need to go in standby mode to modify registers */
293 	ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
294 	if (ret < 0)
295 		return ret;
296 
297 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH);
298 	if (ret < 0)
299 		goto err_activate;
300 
301 	masked_reg = ret & (~STK8312_RNG_MASK);
302 	masked_reg |= range << STK8312_RNG_SHIFT;
303 
304 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg);
305 	if (ret < 0)
306 		goto err_activate;
307 
308 	data->range = range;
309 
310 	return stk8312_set_mode(data, mode);
311 
312 err_activate:
313 	dev_err(&client->dev, "failed to change sensor range\n");
314 	stk8312_set_mode(data, mode);
315 
316 	return ret;
317 }
318 
319 static int stk8312_read_accel(struct stk8312_data *data, u8 address)
320 {
321 	int ret;
322 	struct i2c_client *client = data->client;
323 
324 	if (address > 2)
325 		return -EINVAL;
326 
327 	ret = i2c_smbus_read_byte_data(client, address);
328 	if (ret < 0)
329 		dev_err(&client->dev, "register read failed\n");
330 
331 	return ret;
332 }
333 
334 static int stk8312_read_raw(struct iio_dev *indio_dev,
335 			    struct iio_chan_spec const *chan,
336 			    int *val, int *val2, long mask)
337 {
338 	struct stk8312_data *data = iio_priv(indio_dev);
339 	int ret;
340 
341 	switch (mask) {
342 	case IIO_CHAN_INFO_RAW:
343 		if (iio_buffer_enabled(indio_dev))
344 			return -EBUSY;
345 		mutex_lock(&data->lock);
346 		ret = stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
347 		if (ret < 0) {
348 			mutex_unlock(&data->lock);
349 			return ret;
350 		}
351 		ret = stk8312_read_accel(data, chan->address);
352 		if (ret < 0) {
353 			stk8312_set_mode(data,
354 					 data->mode & (~STK8312_MODE_ACTIVE));
355 			mutex_unlock(&data->lock);
356 			return ret;
357 		}
358 		*val = sign_extend32(ret, chan->scan_type.realbits - 1);
359 		ret = stk8312_set_mode(data,
360 				       data->mode & (~STK8312_MODE_ACTIVE));
361 		mutex_unlock(&data->lock);
362 		if (ret < 0)
363 			return ret;
364 		return IIO_VAL_INT;
365 	case IIO_CHAN_INFO_SCALE:
366 		*val = stk8312_scale_table[data->range - 1][0];
367 		*val2 = stk8312_scale_table[data->range - 1][1];
368 		return IIO_VAL_INT_PLUS_MICRO;
369 	case IIO_CHAN_INFO_SAMP_FREQ:
370 		*val = stk8312_samp_freq_table[data->sample_rate_idx].val;
371 		*val2 = stk8312_samp_freq_table[data->sample_rate_idx].val2;
372 		return IIO_VAL_INT_PLUS_MICRO;
373 	}
374 
375 	return -EINVAL;
376 }
377 
378 static int stk8312_write_raw(struct iio_dev *indio_dev,
379 			     struct iio_chan_spec const *chan,
380 			     int val, int val2, long mask)
381 {
382 	int i;
383 	int index = -1;
384 	int ret;
385 	struct stk8312_data *data = iio_priv(indio_dev);
386 
387 	switch (mask) {
388 	case IIO_CHAN_INFO_SCALE:
389 		for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++)
390 			if (val == stk8312_scale_table[i][0] &&
391 			    val2 == stk8312_scale_table[i][1]) {
392 				index = i + 1;
393 				break;
394 			}
395 		if (index < 0)
396 			return -EINVAL;
397 
398 		mutex_lock(&data->lock);
399 		ret = stk8312_set_range(data, index);
400 		mutex_unlock(&data->lock);
401 
402 		return ret;
403 	case IIO_CHAN_INFO_SAMP_FREQ:
404 		for (i = 0; i < ARRAY_SIZE(stk8312_samp_freq_table); i++)
405 			if (val == stk8312_samp_freq_table[i].val &&
406 			    val2 == stk8312_samp_freq_table[i].val2) {
407 				index = i;
408 				break;
409 			}
410 		if (index < 0)
411 			return -EINVAL;
412 		mutex_lock(&data->lock);
413 		ret = stk8312_set_sample_rate(data, index);
414 		mutex_unlock(&data->lock);
415 
416 		return ret;
417 	}
418 
419 	return -EINVAL;
420 }
421 
422 static const struct iio_info stk8312_info = {
423 	.read_raw		= stk8312_read_raw,
424 	.write_raw		= stk8312_write_raw,
425 	.attrs			= &stk8312_attribute_group,
426 };
427 
428 static irqreturn_t stk8312_trigger_handler(int irq, void *p)
429 {
430 	struct iio_poll_func *pf = p;
431 	struct iio_dev *indio_dev = pf->indio_dev;
432 	struct stk8312_data *data = iio_priv(indio_dev);
433 	int bit, ret, i = 0;
434 
435 	mutex_lock(&data->lock);
436 	/*
437 	 * Do a bulk read if all channels are requested,
438 	 * from 0x00 (XOUT) to 0x02 (ZOUT)
439 	 */
440 	if (*(indio_dev->active_scan_mask) == STK8312_ALL_CHANNEL_MASK) {
441 		ret = i2c_smbus_read_i2c_block_data(data->client,
442 						    STK8312_REG_XOUT,
443 						    STK8312_ALL_CHANNEL_SIZE,
444 						    data->scan.chans);
445 		if (ret < STK8312_ALL_CHANNEL_SIZE) {
446 			dev_err(&data->client->dev, "register read failed\n");
447 			mutex_unlock(&data->lock);
448 			goto err;
449 		}
450 	} else {
451 		for_each_set_bit(bit, indio_dev->active_scan_mask,
452 				 indio_dev->masklength) {
453 			ret = stk8312_read_accel(data, bit);
454 			if (ret < 0) {
455 				mutex_unlock(&data->lock);
456 				goto err;
457 			}
458 			data->scan.chans[i++] = ret;
459 		}
460 	}
461 	mutex_unlock(&data->lock);
462 
463 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
464 					   pf->timestamp);
465 err:
466 	iio_trigger_notify_done(indio_dev->trig);
467 
468 	return IRQ_HANDLED;
469 }
470 
471 static irqreturn_t stk8312_data_rdy_trig_poll(int irq, void *private)
472 {
473 	struct iio_dev *indio_dev = private;
474 	struct stk8312_data *data = iio_priv(indio_dev);
475 
476 	if (data->dready_trigger_on)
477 		iio_trigger_poll(data->dready_trig);
478 
479 	return IRQ_HANDLED;
480 }
481 
482 static int stk8312_buffer_preenable(struct iio_dev *indio_dev)
483 {
484 	struct stk8312_data *data = iio_priv(indio_dev);
485 
486 	return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
487 }
488 
489 static int stk8312_buffer_postdisable(struct iio_dev *indio_dev)
490 {
491 	struct stk8312_data *data = iio_priv(indio_dev);
492 
493 	return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE));
494 }
495 
496 static const struct iio_buffer_setup_ops stk8312_buffer_setup_ops = {
497 	.preenable   = stk8312_buffer_preenable,
498 	.postdisable = stk8312_buffer_postdisable,
499 };
500 
501 static int stk8312_probe(struct i2c_client *client)
502 {
503 	int ret;
504 	struct iio_dev *indio_dev;
505 	struct stk8312_data *data;
506 
507 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
508 	if (!indio_dev) {
509 		dev_err(&client->dev, "iio allocation failed!\n");
510 		return -ENOMEM;
511 	}
512 
513 	data = iio_priv(indio_dev);
514 	data->client = client;
515 	i2c_set_clientdata(client, indio_dev);
516 	mutex_init(&data->lock);
517 
518 	indio_dev->info = &stk8312_info;
519 	indio_dev->name = STK8312_DRIVER_NAME;
520 	indio_dev->modes = INDIO_DIRECT_MODE;
521 	indio_dev->channels = stk8312_channels;
522 	indio_dev->num_channels = ARRAY_SIZE(stk8312_channels);
523 
524 	/* A software reset is recommended at power-on */
525 	ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00);
526 	if (ret < 0) {
527 		dev_err(&client->dev, "failed to reset sensor\n");
528 		return ret;
529 	}
530 	data->sample_rate_idx = STK8312_SR_400HZ_IDX;
531 	ret = stk8312_set_range(data, STK8312_RNG_6G);
532 	if (ret < 0)
533 		return ret;
534 
535 	ret = stk8312_set_mode(data,
536 			       STK8312_MODE_INT_AH_PP | STK8312_MODE_ACTIVE);
537 	if (ret < 0)
538 		return ret;
539 
540 	if (client->irq > 0) {
541 		ret = devm_request_threaded_irq(&client->dev, client->irq,
542 						stk8312_data_rdy_trig_poll,
543 						NULL,
544 						IRQF_TRIGGER_RISING |
545 						IRQF_ONESHOT,
546 						STK8312_IRQ_NAME,
547 						indio_dev);
548 		if (ret < 0) {
549 			dev_err(&client->dev, "request irq %d failed\n",
550 				client->irq);
551 			goto err_power_off;
552 		}
553 
554 		data->dready_trig = devm_iio_trigger_alloc(&client->dev,
555 							   "%s-dev%d",
556 							   indio_dev->name,
557 							   iio_device_id(indio_dev));
558 		if (!data->dready_trig) {
559 			ret = -ENOMEM;
560 			goto err_power_off;
561 		}
562 
563 		data->dready_trig->ops = &stk8312_trigger_ops;
564 		iio_trigger_set_drvdata(data->dready_trig, indio_dev);
565 		ret = iio_trigger_register(data->dready_trig);
566 		if (ret) {
567 			dev_err(&client->dev, "iio trigger register failed\n");
568 			goto err_power_off;
569 		}
570 	}
571 
572 	ret = iio_triggered_buffer_setup(indio_dev,
573 					 iio_pollfunc_store_time,
574 					 stk8312_trigger_handler,
575 					 &stk8312_buffer_setup_ops);
576 	if (ret < 0) {
577 		dev_err(&client->dev, "iio triggered buffer setup failed\n");
578 		goto err_trigger_unregister;
579 	}
580 
581 	ret = iio_device_register(indio_dev);
582 	if (ret < 0) {
583 		dev_err(&client->dev, "device_register failed\n");
584 		goto err_buffer_cleanup;
585 	}
586 
587 	return 0;
588 
589 err_buffer_cleanup:
590 	iio_triggered_buffer_cleanup(indio_dev);
591 err_trigger_unregister:
592 	if (data->dready_trig)
593 		iio_trigger_unregister(data->dready_trig);
594 err_power_off:
595 	stk8312_set_mode(data, STK8312_MODE_STANDBY);
596 	return ret;
597 }
598 
599 static void stk8312_remove(struct i2c_client *client)
600 {
601 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
602 	struct stk8312_data *data = iio_priv(indio_dev);
603 
604 	iio_device_unregister(indio_dev);
605 	iio_triggered_buffer_cleanup(indio_dev);
606 
607 	if (data->dready_trig)
608 		iio_trigger_unregister(data->dready_trig);
609 
610 	stk8312_set_mode(data, STK8312_MODE_STANDBY);
611 }
612 
613 static int stk8312_suspend(struct device *dev)
614 {
615 	struct stk8312_data *data;
616 
617 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
618 
619 	return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE));
620 }
621 
622 static int stk8312_resume(struct device *dev)
623 {
624 	struct stk8312_data *data;
625 
626 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
627 
628 	return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
629 }
630 
631 static DEFINE_SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend,
632 				stk8312_resume);
633 
634 static const struct i2c_device_id stk8312_i2c_id[] = {
635 	/* Deprecated in favour of lowercase form */
636 	{ "STK8312" },
637 	{ "stk8312" },
638 	{}
639 };
640 MODULE_DEVICE_TABLE(i2c, stk8312_i2c_id);
641 
642 static struct i2c_driver stk8312_driver = {
643 	.driver = {
644 		.name = STK8312_DRIVER_NAME,
645 		.pm = pm_sleep_ptr(&stk8312_pm_ops),
646 	},
647 	.probe =        stk8312_probe,
648 	.remove =           stk8312_remove,
649 	.id_table =         stk8312_i2c_id,
650 };
651 
652 module_i2c_driver(stk8312_driver);
653 
654 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
655 MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver");
656 MODULE_LICENSE("GPL v2");
657