xref: /linux/drivers/iio/accel/stk8312.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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/types.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 
23 #define STK8312_REG_XOUT		0x00
24 #define STK8312_REG_YOUT		0x01
25 #define STK8312_REG_ZOUT		0x02
26 #define STK8312_REG_INTSU		0x06
27 #define STK8312_REG_MODE		0x07
28 #define STK8312_REG_SR			0x08
29 #define STK8312_REG_STH			0x13
30 #define STK8312_REG_RESET		0x20
31 #define STK8312_REG_AFECTRL		0x24
32 #define STK8312_REG_OTPADDR		0x3D
33 #define STK8312_REG_OTPDATA		0x3E
34 #define STK8312_REG_OTPCTRL		0x3F
35 
36 #define STK8312_MODE_ACTIVE		BIT(0)
37 #define STK8312_MODE_STANDBY		0x00
38 #define STK8312_MODE_INT_AH_PP		0xC0	/* active-high, push-pull */
39 #define STK8312_DREADY_BIT		BIT(4)
40 #define STK8312_RNG_6G			1
41 #define STK8312_RNG_SHIFT		6
42 #define STK8312_RNG_MASK		GENMASK(7, 6)
43 #define STK8312_SR_MASK			GENMASK(2, 0)
44 #define STK8312_SR_400HZ_IDX		0
45 #define STK8312_ALL_CHANNEL_MASK	GENMASK(2, 0)
46 #define STK8312_ALL_CHANNEL_SIZE	3
47 
48 #define STK8312_DRIVER_NAME		"stk8312"
49 #define STK8312_IRQ_NAME		"stk8312_event"
50 
51 /*
52  * The accelerometer has two measurement ranges:
53  *
54  * -6g - +6g (8-bit, signed)
55  * -16g - +16g (8-bit, signed)
56  *
57  * scale1 = (6 + 6) * 9.81 / (2^8 - 1)     = 0.4616
58  * scale2 = (16 + 16) * 9.81 / (2^8 - 1)   = 1.2311
59  */
60 #define STK8312_SCALE_AVAIL		"0.4616 1.2311"
61 
62 static const int stk8312_scale_table[][2] = {
63 	{0, 461600}, {1, 231100}
64 };
65 
66 static const struct {
67 	int val;
68 	int val2;
69 } stk8312_samp_freq_table[] = {
70 	{400, 0}, {200, 0}, {100, 0}, {50, 0}, {25, 0},
71 	{12, 500000}, {6, 250000}, {3, 125000}
72 };
73 
74 #define STK8312_ACCEL_CHANNEL(index, reg, axis) {			\
75 	.type = IIO_ACCEL,						\
76 	.address = reg,							\
77 	.modified = 1,							\
78 	.channel2 = IIO_MOD_##axis,					\
79 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
80 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
81 				    BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
82 	.scan_index = index,						\
83 	.scan_type = {							\
84 		.sign = 's',						\
85 		.realbits = 8,						\
86 		.storagebits = 8,					\
87 		.endianness = IIO_CPU,					\
88 	},								\
89 }
90 
91 static const struct iio_chan_spec stk8312_channels[] = {
92 	STK8312_ACCEL_CHANNEL(0, STK8312_REG_XOUT, X),
93 	STK8312_ACCEL_CHANNEL(1, STK8312_REG_YOUT, Y),
94 	STK8312_ACCEL_CHANNEL(2, STK8312_REG_ZOUT, Z),
95 	IIO_CHAN_SOFT_TIMESTAMP(3),
96 };
97 
98 struct stk8312_data {
99 	struct i2c_client *client;
100 	struct mutex lock;
101 	u8 range;
102 	u8 sample_rate_idx;
103 	u8 mode;
104 	struct iio_trigger *dready_trig;
105 	bool dready_trigger_on;
106 	/* Ensure timestamp is naturally aligned */
107 	struct {
108 		s8 chans[3];
109 		aligned_s64 timestamp;
110 	} scan;
111 };
112 
113 static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL);
114 
115 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400");
116 
117 static struct attribute *stk8312_attributes[] = {
118 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
119 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
120 	NULL,
121 };
122 
123 static const struct attribute_group stk8312_attribute_group = {
124 	.attrs = stk8312_attributes
125 };
126 
127 static int stk8312_otp_init(struct stk8312_data *data)
128 {
129 	int ret;
130 	int count = 10;
131 	struct i2c_client *client = data->client;
132 
133 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70);
134 	if (ret < 0)
135 		goto exit_err;
136 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02);
137 	if (ret < 0)
138 		goto exit_err;
139 
140 	do {
141 		usleep_range(1000, 5000);
142 		ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL);
143 		if (ret < 0)
144 			goto exit_err;
145 		count--;
146 	} while (!(ret & BIT(7)) && count > 0);
147 
148 	if (count == 0) {
149 		ret = -ETIMEDOUT;
150 		goto exit_err;
151 	}
152 
153 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA);
154 	if (ret == 0)
155 		ret = -EINVAL;
156 	if (ret < 0)
157 		goto exit_err;
158 
159 	ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_AFECTRL, ret);
160 	if (ret < 0)
161 		goto exit_err;
162 	msleep(150);
163 
164 	return 0;
165 
166 exit_err:
167 	dev_err(&client->dev, "failed to initialize sensor\n");
168 	return ret;
169 }
170 
171 static int stk8312_set_mode(struct stk8312_data *data, u8 mode)
172 {
173 	int ret;
174 	struct i2c_client *client = data->client;
175 
176 	if (mode == data->mode)
177 		return 0;
178 
179 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_MODE, mode);
180 	if (ret < 0) {
181 		dev_err(&client->dev, "failed to change sensor mode\n");
182 		return ret;
183 	}
184 
185 	data->mode = mode;
186 	if (mode & STK8312_MODE_ACTIVE) {
187 		/* Need to run OTP sequence before entering active mode */
188 		usleep_range(1000, 5000);
189 		ret = stk8312_otp_init(data);
190 	}
191 
192 	return ret;
193 }
194 
195 static int stk8312_set_interrupts(struct stk8312_data *data, u8 int_mask)
196 {
197 	int ret;
198 	u8 mode;
199 	struct i2c_client *client = data->client;
200 
201 	mode = data->mode;
202 	/* We need to go in standby mode to modify registers */
203 	ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
204 	if (ret < 0)
205 		return ret;
206 
207 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_INTSU, int_mask);
208 	if (ret < 0) {
209 		dev_err(&client->dev, "failed to set interrupts\n");
210 		stk8312_set_mode(data, mode);
211 		return ret;
212 	}
213 
214 	return stk8312_set_mode(data, mode);
215 }
216 
217 static int stk8312_data_rdy_trigger_set_state(struct iio_trigger *trig,
218 					      bool state)
219 {
220 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
221 	struct stk8312_data *data = iio_priv(indio_dev);
222 	int ret;
223 
224 	if (state)
225 		ret = stk8312_set_interrupts(data, STK8312_DREADY_BIT);
226 	else
227 		ret = stk8312_set_interrupts(data, 0x00);
228 
229 	if (ret < 0) {
230 		dev_err(&data->client->dev, "failed to set trigger state\n");
231 		return ret;
232 	}
233 
234 	data->dready_trigger_on = state;
235 
236 	return 0;
237 }
238 
239 static const struct iio_trigger_ops stk8312_trigger_ops = {
240 	.set_trigger_state = stk8312_data_rdy_trigger_set_state,
241 };
242 
243 static int stk8312_set_sample_rate(struct stk8312_data *data, u8 rate)
244 {
245 	int ret;
246 	u8 masked_reg;
247 	u8 mode;
248 	struct i2c_client *client = data->client;
249 
250 	if (rate == data->sample_rate_idx)
251 		return 0;
252 
253 	mode = data->mode;
254 	/* We need to go in standby mode to modify registers */
255 	ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
256 	if (ret < 0)
257 		return ret;
258 
259 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_SR);
260 	if (ret < 0)
261 		goto err_activate;
262 
263 	masked_reg = (ret & (~STK8312_SR_MASK)) | rate;
264 
265 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_SR, masked_reg);
266 	if (ret < 0)
267 		goto err_activate;
268 
269 	data->sample_rate_idx = rate;
270 
271 	return stk8312_set_mode(data, mode);
272 
273 err_activate:
274 	dev_err(&client->dev, "failed to set sampling rate\n");
275 	stk8312_set_mode(data, mode);
276 
277 	return ret;
278 }
279 
280 static int stk8312_set_range(struct stk8312_data *data, u8 range)
281 {
282 	int ret;
283 	u8 masked_reg;
284 	u8 mode;
285 	struct i2c_client *client = data->client;
286 
287 	if (range != 1 && range != 2)
288 		return -EINVAL;
289 	else if (range == data->range)
290 		return 0;
291 
292 	mode = data->mode;
293 	/* We need to go in standby mode to modify registers */
294 	ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
295 	if (ret < 0)
296 		return ret;
297 
298 	ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH);
299 	if (ret < 0)
300 		goto err_activate;
301 
302 	masked_reg = ret & (~STK8312_RNG_MASK);
303 	masked_reg |= range << STK8312_RNG_SHIFT;
304 
305 	ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg);
306 	if (ret < 0)
307 		goto err_activate;
308 
309 	data->range = range;
310 
311 	return stk8312_set_mode(data, mode);
312 
313 err_activate:
314 	dev_err(&client->dev, "failed to change sensor range\n");
315 	stk8312_set_mode(data, mode);
316 
317 	return ret;
318 }
319 
320 static int stk8312_read_accel(struct stk8312_data *data, u8 address)
321 {
322 	int ret;
323 	struct i2c_client *client = data->client;
324 
325 	if (address > 2)
326 		return -EINVAL;
327 
328 	ret = i2c_smbus_read_byte_data(client, address);
329 	if (ret < 0)
330 		dev_err(&client->dev, "register read failed\n");
331 
332 	return ret;
333 }
334 
335 static int stk8312_read_raw(struct iio_dev *indio_dev,
336 			    struct iio_chan_spec const *chan,
337 			    int *val, int *val2, long mask)
338 {
339 	struct stk8312_data *data = iio_priv(indio_dev);
340 	int ret;
341 
342 	switch (mask) {
343 	case IIO_CHAN_INFO_RAW:
344 		if (iio_buffer_enabled(indio_dev))
345 			return -EBUSY;
346 		mutex_lock(&data->lock);
347 		ret = stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
348 		if (ret < 0) {
349 			mutex_unlock(&data->lock);
350 			return ret;
351 		}
352 		ret = stk8312_read_accel(data, chan->address);
353 		if (ret < 0) {
354 			stk8312_set_mode(data,
355 					 data->mode & (~STK8312_MODE_ACTIVE));
356 			mutex_unlock(&data->lock);
357 			return ret;
358 		}
359 		*val = sign_extend32(ret, chan->scan_type.realbits - 1);
360 		ret = stk8312_set_mode(data,
361 				       data->mode & (~STK8312_MODE_ACTIVE));
362 		mutex_unlock(&data->lock);
363 		if (ret < 0)
364 			return ret;
365 		return IIO_VAL_INT;
366 	case IIO_CHAN_INFO_SCALE:
367 		*val = stk8312_scale_table[data->range - 1][0];
368 		*val2 = stk8312_scale_table[data->range - 1][1];
369 		return IIO_VAL_INT_PLUS_MICRO;
370 	case IIO_CHAN_INFO_SAMP_FREQ:
371 		*val = stk8312_samp_freq_table[data->sample_rate_idx].val;
372 		*val2 = stk8312_samp_freq_table[data->sample_rate_idx].val2;
373 		return IIO_VAL_INT_PLUS_MICRO;
374 	}
375 
376 	return -EINVAL;
377 }
378 
379 static int stk8312_write_raw(struct iio_dev *indio_dev,
380 			     struct iio_chan_spec const *chan,
381 			     int val, int val2, long mask)
382 {
383 	int i;
384 	int index = -1;
385 	int ret;
386 	struct stk8312_data *data = iio_priv(indio_dev);
387 
388 	switch (mask) {
389 	case IIO_CHAN_INFO_SCALE:
390 		for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++)
391 			if (val == stk8312_scale_table[i][0] &&
392 			    val2 == stk8312_scale_table[i][1]) {
393 				index = i + 1;
394 				break;
395 			}
396 		if (index < 0)
397 			return -EINVAL;
398 
399 		mutex_lock(&data->lock);
400 		ret = stk8312_set_range(data, index);
401 		mutex_unlock(&data->lock);
402 
403 		return ret;
404 	case IIO_CHAN_INFO_SAMP_FREQ:
405 		for (i = 0; i < ARRAY_SIZE(stk8312_samp_freq_table); i++)
406 			if (val == stk8312_samp_freq_table[i].val &&
407 			    val2 == stk8312_samp_freq_table[i].val2) {
408 				index = i;
409 				break;
410 			}
411 		if (index < 0)
412 			return -EINVAL;
413 		mutex_lock(&data->lock);
414 		ret = stk8312_set_sample_rate(data, index);
415 		mutex_unlock(&data->lock);
416 
417 		return ret;
418 	}
419 
420 	return -EINVAL;
421 }
422 
423 static const struct iio_info stk8312_info = {
424 	.read_raw		= stk8312_read_raw,
425 	.write_raw		= stk8312_write_raw,
426 	.attrs			= &stk8312_attribute_group,
427 };
428 
429 static irqreturn_t stk8312_trigger_handler(int irq, void *p)
430 {
431 	struct iio_poll_func *pf = p;
432 	struct iio_dev *indio_dev = pf->indio_dev;
433 	struct stk8312_data *data = iio_priv(indio_dev);
434 	int bit, ret, i = 0;
435 
436 	mutex_lock(&data->lock);
437 	/*
438 	 * Do a bulk read if all channels are requested,
439 	 * from 0x00 (XOUT) to 0x02 (ZOUT)
440 	 */
441 	if (*(indio_dev->active_scan_mask) == STK8312_ALL_CHANNEL_MASK) {
442 		ret = i2c_smbus_read_i2c_block_data(data->client,
443 						    STK8312_REG_XOUT,
444 						    STK8312_ALL_CHANNEL_SIZE,
445 						    data->scan.chans);
446 		if (ret < STK8312_ALL_CHANNEL_SIZE) {
447 			dev_err(&data->client->dev, "register read failed\n");
448 			mutex_unlock(&data->lock);
449 			goto err;
450 		}
451 	} else {
452 		iio_for_each_active_channel(indio_dev, bit) {
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