xref: /linux/drivers/iio/accel/stk8ba50.c (revision 3ca3af7d1f230d1f93ba4cd8cd9d054870f2406f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sensortek STK8BA50 3-Axis Accelerometer
4  *
5  * Copyright (c) 2015, Intel Corporation.
6  *
7  * STK8BA50 7-bit I2C address: 0x18.
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.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 STK8BA50_REG_XOUT			0x02
23 #define STK8BA50_REG_YOUT			0x04
24 #define STK8BA50_REG_ZOUT			0x06
25 #define STK8BA50_REG_RANGE			0x0F
26 #define STK8BA50_REG_BWSEL			0x10
27 #define STK8BA50_REG_POWMODE			0x11
28 #define STK8BA50_REG_SWRST			0x14
29 #define STK8BA50_REG_INTEN2			0x17
30 #define STK8BA50_REG_INTMAP2			0x1A
31 
32 #define STK8BA50_MODE_NORMAL			0
33 #define STK8BA50_MODE_SUSPEND			1
34 #define STK8BA50_MODE_POWERBIT			BIT(7)
35 #define STK8BA50_DATA_SHIFT			6
36 #define STK8BA50_RESET_CMD			0xB6
37 #define STK8BA50_SR_1792HZ_IDX			7
38 #define STK8BA50_DREADY_INT_MASK		0x10
39 #define STK8BA50_DREADY_INT_MAP			0x81
40 #define STK8BA50_ALL_CHANNEL_MASK		7
41 #define STK8BA50_ALL_CHANNEL_SIZE		6
42 
43 #define STK8BA50_DRIVER_NAME			"stk8ba50"
44 #define STK8BA50_IRQ_NAME			"stk8ba50_event"
45 
46 #define STK8BA50_SCALE_AVAIL			"0.0384 0.0767 0.1534 0.3069"
47 
48 /*
49  * The accelerometer has four measurement ranges:
50  * +/-2g; +/-4g; +/-8g; +/-16g
51  *
52  * Acceleration values are 10-bit, 2's complement.
53  * Scales are calculated as following:
54  *
55  * scale1 = (2 + 2) * 9.81 / (2^10 - 1)   = 0.0384
56  * scale2 = (4 + 4) * 9.81 / (2^10 - 1)   = 0.0767
57  * etc.
58  *
59  * Scales are stored in this format:
60  * { <register value>, <scale value> }
61  *
62  * Locally, the range is stored as a table index.
63  */
64 static const struct {
65 	u8 reg_val;
66 	u32 scale_val;
67 } stk8ba50_scale_table[] = {
68 	{3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
69 };
70 
71 /* Sample rates are stored as { <register value>, <Hz value> } */
72 static const struct {
73 	u8 reg_val;
74 	u16 samp_freq;
75 } stk8ba50_samp_freq_table[] = {
76 	{0x08, 14},  {0x09, 25},  {0x0A, 56},  {0x0B, 112},
77 	{0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
78 };
79 
80 /* Used to map scan mask bits to their corresponding channel register. */
81 static const int stk8ba50_channel_table[] = {
82 	STK8BA50_REG_XOUT,
83 	STK8BA50_REG_YOUT,
84 	STK8BA50_REG_ZOUT
85 };
86 
87 struct stk8ba50_data {
88 	struct i2c_client *client;
89 	struct mutex lock;
90 	int range;
91 	u8 sample_rate_idx;
92 	struct iio_trigger *dready_trig;
93 	bool dready_trigger_on;
94 	/* Ensure timestamp is naturally aligned */
95 	struct {
96 		s16 chans[3];
97 		s64 timetamp __aligned(8);
98 	} scan;
99 };
100 
101 #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) {			\
102 	.type = IIO_ACCEL,						\
103 	.address = reg,							\
104 	.modified = 1,							\
105 	.channel2 = IIO_MOD_##axis,					\
106 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
107 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
108 				    BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
109 	.scan_index = index,						\
110 	.scan_type = {							\
111 		.sign = 's',						\
112 		.realbits = 10,						\
113 		.storagebits = 16,					\
114 		.shift = STK8BA50_DATA_SHIFT,				\
115 		.endianness = IIO_CPU,					\
116 	},								\
117 }
118 
119 static const struct iio_chan_spec stk8ba50_channels[] = {
120 	STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT, X),
121 	STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT, Y),
122 	STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT, Z),
123 	IIO_CHAN_SOFT_TIMESTAMP(3),
124 };
125 
126 static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL);
127 
128 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
129 
130 static struct attribute *stk8ba50_attributes[] = {
131 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
132 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
133 	NULL,
134 };
135 
136 static const struct attribute_group stk8ba50_attribute_group = {
137 	.attrs = stk8ba50_attributes
138 };
139 
140 static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg)
141 {
142 	int ret;
143 	struct i2c_client *client = data->client;
144 
145 	ret = i2c_smbus_read_word_data(client, reg);
146 	if (ret < 0) {
147 		dev_err(&client->dev, "register read failed\n");
148 		return ret;
149 	}
150 
151 	return ret;
152 }
153 
154 static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger *trig,
155 					       bool state)
156 {
157 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
158 	struct stk8ba50_data *data = iio_priv(indio_dev);
159 	int ret;
160 
161 	if (state)
162 		ret = i2c_smbus_write_byte_data(data->client,
163 			STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
164 	else
165 		ret = i2c_smbus_write_byte_data(data->client,
166 			STK8BA50_REG_INTEN2, 0x00);
167 
168 	if (ret < 0)
169 		dev_err(&data->client->dev, "failed to set trigger state\n");
170 	else
171 		data->dready_trigger_on = state;
172 
173 	return ret;
174 }
175 
176 static const struct iio_trigger_ops stk8ba50_trigger_ops = {
177 	.set_trigger_state = stk8ba50_data_rdy_trigger_set_state,
178 };
179 
180 static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode)
181 {
182 	int ret;
183 	u8 masked_reg;
184 	struct i2c_client *client = data->client;
185 
186 	ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE);
187 	if (ret < 0)
188 		goto exit_err;
189 
190 	if (mode)
191 		masked_reg = ret | STK8BA50_MODE_POWERBIT;
192 	else
193 		masked_reg = ret & (~STK8BA50_MODE_POWERBIT);
194 
195 	ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE,
196 					masked_reg);
197 	if (ret < 0)
198 		goto exit_err;
199 
200 	return ret;
201 
202 exit_err:
203 	dev_err(&client->dev, "failed to change sensor mode\n");
204 	return ret;
205 }
206 
207 static int stk8ba50_read_raw(struct iio_dev *indio_dev,
208 			     struct iio_chan_spec const *chan,
209 			     int *val, int *val2, long mask)
210 {
211 	struct stk8ba50_data *data = iio_priv(indio_dev);
212 	int ret;
213 
214 	switch (mask) {
215 	case IIO_CHAN_INFO_RAW:
216 		if (iio_buffer_enabled(indio_dev))
217 			return -EBUSY;
218 		mutex_lock(&data->lock);
219 		ret = stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
220 		if (ret < 0) {
221 			mutex_unlock(&data->lock);
222 			return -EINVAL;
223 		}
224 		ret = stk8ba50_read_accel(data, chan->address);
225 		if (ret < 0) {
226 			stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
227 			mutex_unlock(&data->lock);
228 			return -EINVAL;
229 		}
230 		*val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9);
231 		stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
232 		mutex_unlock(&data->lock);
233 		return IIO_VAL_INT;
234 	case IIO_CHAN_INFO_SCALE:
235 		*val = 0;
236 		*val2 = stk8ba50_scale_table[data->range].scale_val;
237 		return IIO_VAL_INT_PLUS_MICRO;
238 	case IIO_CHAN_INFO_SAMP_FREQ:
239 		*val = stk8ba50_samp_freq_table
240 				[data->sample_rate_idx].samp_freq;
241 		*val2 = 0;
242 		return IIO_VAL_INT;
243 	}
244 
245 	return -EINVAL;
246 }
247 
248 static int stk8ba50_write_raw(struct iio_dev *indio_dev,
249 			      struct iio_chan_spec const *chan,
250 			      int val, int val2, long mask)
251 {
252 	int ret;
253 	int i;
254 	int index = -1;
255 	struct stk8ba50_data *data = iio_priv(indio_dev);
256 
257 	switch (mask) {
258 	case IIO_CHAN_INFO_SCALE:
259 		if (val != 0)
260 			return -EINVAL;
261 
262 		for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++)
263 			if (val2 == stk8ba50_scale_table[i].scale_val) {
264 				index = i;
265 				break;
266 			}
267 		if (index < 0)
268 			return -EINVAL;
269 
270 		ret = i2c_smbus_write_byte_data(data->client,
271 				STK8BA50_REG_RANGE,
272 				stk8ba50_scale_table[index].reg_val);
273 		if (ret < 0)
274 			dev_err(&data->client->dev,
275 					"failed to set measurement range\n");
276 		else
277 			data->range = index;
278 
279 		return ret;
280 	case IIO_CHAN_INFO_SAMP_FREQ:
281 		for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++)
282 			if (val == stk8ba50_samp_freq_table[i].samp_freq) {
283 				index = i;
284 				break;
285 			}
286 		if (index < 0)
287 			return -EINVAL;
288 
289 		ret = i2c_smbus_write_byte_data(data->client,
290 				STK8BA50_REG_BWSEL,
291 				stk8ba50_samp_freq_table[index].reg_val);
292 		if (ret < 0)
293 			dev_err(&data->client->dev,
294 					"failed to set sampling rate\n");
295 		else
296 			data->sample_rate_idx = index;
297 
298 		return ret;
299 	}
300 
301 	return -EINVAL;
302 }
303 
304 static const struct iio_info stk8ba50_info = {
305 	.read_raw		= stk8ba50_read_raw,
306 	.write_raw		= stk8ba50_write_raw,
307 	.attrs			= &stk8ba50_attribute_group,
308 };
309 
310 static irqreturn_t stk8ba50_trigger_handler(int irq, void *p)
311 {
312 	struct iio_poll_func *pf = p;
313 	struct iio_dev *indio_dev = pf->indio_dev;
314 	struct stk8ba50_data *data = iio_priv(indio_dev);
315 	int bit, ret, i = 0;
316 
317 	mutex_lock(&data->lock);
318 	/*
319 	 * Do a bulk read if all channels are requested,
320 	 * from 0x02 (XOUT1) to 0x07 (ZOUT2)
321 	 */
322 	if (*(indio_dev->active_scan_mask) == STK8BA50_ALL_CHANNEL_MASK) {
323 		ret = i2c_smbus_read_i2c_block_data(data->client,
324 						    STK8BA50_REG_XOUT,
325 						    STK8BA50_ALL_CHANNEL_SIZE,
326 						    (u8 *)data->scan.chans);
327 		if (ret < STK8BA50_ALL_CHANNEL_SIZE) {
328 			dev_err(&data->client->dev, "register read failed\n");
329 			goto err;
330 		}
331 	} else {
332 		for_each_set_bit(bit, indio_dev->active_scan_mask,
333 				 indio_dev->masklength) {
334 			ret = stk8ba50_read_accel(data,
335 						  stk8ba50_channel_table[bit]);
336 			if (ret < 0)
337 				goto err;
338 
339 			data->scan.chans[i++] = ret;
340 		}
341 	}
342 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
343 					   pf->timestamp);
344 err:
345 	mutex_unlock(&data->lock);
346 	iio_trigger_notify_done(indio_dev->trig);
347 
348 	return IRQ_HANDLED;
349 }
350 
351 static irqreturn_t stk8ba50_data_rdy_trig_poll(int irq, void *private)
352 {
353 	struct iio_dev *indio_dev = private;
354 	struct stk8ba50_data *data = iio_priv(indio_dev);
355 
356 	if (data->dready_trigger_on)
357 		iio_trigger_poll(data->dready_trig);
358 
359 	return IRQ_HANDLED;
360 }
361 
362 static int stk8ba50_buffer_preenable(struct iio_dev *indio_dev)
363 {
364 	struct stk8ba50_data *data = iio_priv(indio_dev);
365 
366 	return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
367 }
368 
369 static int stk8ba50_buffer_postdisable(struct iio_dev *indio_dev)
370 {
371 	struct stk8ba50_data *data = iio_priv(indio_dev);
372 
373 	return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
374 }
375 
376 static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops = {
377 	.preenable   = stk8ba50_buffer_preenable,
378 	.postdisable = stk8ba50_buffer_postdisable,
379 };
380 
381 static int stk8ba50_probe(struct i2c_client *client,
382 			  const struct i2c_device_id *id)
383 {
384 	int ret;
385 	struct iio_dev *indio_dev;
386 	struct stk8ba50_data *data;
387 
388 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
389 	if (!indio_dev) {
390 		dev_err(&client->dev, "iio allocation failed!\n");
391 		return -ENOMEM;
392 	}
393 
394 	data = iio_priv(indio_dev);
395 	data->client = client;
396 	i2c_set_clientdata(client, indio_dev);
397 	mutex_init(&data->lock);
398 
399 	indio_dev->info = &stk8ba50_info;
400 	indio_dev->name = STK8BA50_DRIVER_NAME;
401 	indio_dev->modes = INDIO_DIRECT_MODE;
402 	indio_dev->channels = stk8ba50_channels;
403 	indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels);
404 
405 	/* Reset all registers on startup */
406 	ret = i2c_smbus_write_byte_data(client,
407 			STK8BA50_REG_SWRST, STK8BA50_RESET_CMD);
408 	if (ret < 0) {
409 		dev_err(&client->dev, "failed to reset sensor\n");
410 		goto err_power_off;
411 	}
412 
413 	/* The default range is +/-2g */
414 	data->range = 0;
415 
416 	/* The default sampling rate is 1792 Hz (maximum) */
417 	data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX;
418 
419 	/* Set up interrupts */
420 	ret = i2c_smbus_write_byte_data(client,
421 			STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
422 	if (ret < 0) {
423 		dev_err(&client->dev, "failed to set up interrupts\n");
424 		goto err_power_off;
425 	}
426 	ret = i2c_smbus_write_byte_data(client,
427 			STK8BA50_REG_INTMAP2, STK8BA50_DREADY_INT_MAP);
428 	if (ret < 0) {
429 		dev_err(&client->dev, "failed to set up interrupts\n");
430 		goto err_power_off;
431 	}
432 
433 	if (client->irq > 0) {
434 		ret = devm_request_threaded_irq(&client->dev, client->irq,
435 						stk8ba50_data_rdy_trig_poll,
436 						NULL,
437 						IRQF_TRIGGER_RISING |
438 						IRQF_ONESHOT,
439 						STK8BA50_IRQ_NAME,
440 						indio_dev);
441 		if (ret < 0) {
442 			dev_err(&client->dev, "request irq %d failed\n",
443 				client->irq);
444 			goto err_power_off;
445 		}
446 
447 		data->dready_trig = devm_iio_trigger_alloc(&client->dev,
448 							   "%s-dev%d",
449 							   indio_dev->name,
450 							   iio_device_id(indio_dev));
451 		if (!data->dready_trig) {
452 			ret = -ENOMEM;
453 			goto err_power_off;
454 		}
455 
456 		data->dready_trig->ops = &stk8ba50_trigger_ops;
457 		iio_trigger_set_drvdata(data->dready_trig, indio_dev);
458 		ret = iio_trigger_register(data->dready_trig);
459 		if (ret) {
460 			dev_err(&client->dev, "iio trigger register failed\n");
461 			goto err_power_off;
462 		}
463 	}
464 
465 	ret = iio_triggered_buffer_setup(indio_dev,
466 					 iio_pollfunc_store_time,
467 					 stk8ba50_trigger_handler,
468 					 &stk8ba50_buffer_setup_ops);
469 	if (ret < 0) {
470 		dev_err(&client->dev, "iio triggered buffer setup failed\n");
471 		goto err_trigger_unregister;
472 	}
473 
474 	ret = iio_device_register(indio_dev);
475 	if (ret < 0) {
476 		dev_err(&client->dev, "device_register failed\n");
477 		goto err_buffer_cleanup;
478 	}
479 
480 	return ret;
481 
482 err_buffer_cleanup:
483 	iio_triggered_buffer_cleanup(indio_dev);
484 err_trigger_unregister:
485 	if (data->dready_trig)
486 		iio_trigger_unregister(data->dready_trig);
487 err_power_off:
488 	stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
489 	return ret;
490 }
491 
492 static int stk8ba50_remove(struct i2c_client *client)
493 {
494 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
495 	struct stk8ba50_data *data = iio_priv(indio_dev);
496 
497 	iio_device_unregister(indio_dev);
498 	iio_triggered_buffer_cleanup(indio_dev);
499 
500 	if (data->dready_trig)
501 		iio_trigger_unregister(data->dready_trig);
502 
503 	return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
504 }
505 
506 #ifdef CONFIG_PM_SLEEP
507 static int stk8ba50_suspend(struct device *dev)
508 {
509 	struct stk8ba50_data *data;
510 
511 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
512 
513 	return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
514 }
515 
516 static int stk8ba50_resume(struct device *dev)
517 {
518 	struct stk8ba50_data *data;
519 
520 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
521 
522 	return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
523 }
524 
525 static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume);
526 
527 #define STK8BA50_PM_OPS (&stk8ba50_pm_ops)
528 #else
529 #define STK8BA50_PM_OPS NULL
530 #endif
531 
532 static const struct i2c_device_id stk8ba50_i2c_id[] = {
533 	{"stk8ba50", 0},
534 	{}
535 };
536 MODULE_DEVICE_TABLE(i2c, stk8ba50_i2c_id);
537 
538 static const struct acpi_device_id stk8ba50_acpi_id[] = {
539 	{"STK8BA50", 0},
540 	{}
541 };
542 
543 MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id);
544 
545 static struct i2c_driver stk8ba50_driver = {
546 	.driver = {
547 		.name = "stk8ba50",
548 		.pm = STK8BA50_PM_OPS,
549 		.acpi_match_table = ACPI_PTR(stk8ba50_acpi_id),
550 	},
551 	.probe =            stk8ba50_probe,
552 	.remove =           stk8ba50_remove,
553 	.id_table =         stk8ba50_i2c_id,
554 };
555 
556 module_i2c_driver(stk8ba50_driver);
557 
558 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
559 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
560 MODULE_LICENSE("GPL v2");
561