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