1 /*
2  * STMicroelectronics sensors core library driver
3  *
4  * Copyright 2012-2013 STMicroelectronics Inc.
5  *
6  * Denis Ciocca <denis.ciocca@st.com>
7  *
8  * Licensed under the GPL-2.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/iio/iio.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <asm/unaligned.h>
20 #include <linux/iio/common/st_sensors.h>
21 
22 #include "st_sensors_core.h"
23 
st_sensors_get_unaligned_le24(const u8 * p)24 static inline u32 st_sensors_get_unaligned_le24(const u8 *p)
25 {
26 	return (s32)((p[0] | p[1] << 8 | p[2] << 16) << 8) >> 8;
27 }
28 
st_sensors_write_data_with_mask(struct iio_dev * indio_dev,u8 reg_addr,u8 mask,u8 data)29 int st_sensors_write_data_with_mask(struct iio_dev *indio_dev,
30 				    u8 reg_addr, u8 mask, u8 data)
31 {
32 	int err;
33 	u8 new_data;
34 	struct st_sensor_data *sdata = iio_priv(indio_dev);
35 
36 	err = sdata->tf->read_byte(&sdata->tb, sdata->dev, reg_addr, &new_data);
37 	if (err < 0)
38 		goto st_sensors_write_data_with_mask_error;
39 
40 	new_data = ((new_data & (~mask)) | ((data << __ffs(mask)) & mask));
41 	err = sdata->tf->write_byte(&sdata->tb, sdata->dev, reg_addr, new_data);
42 
43 st_sensors_write_data_with_mask_error:
44 	return err;
45 }
46 
st_sensors_debugfs_reg_access(struct iio_dev * indio_dev,unsigned reg,unsigned writeval,unsigned * readval)47 int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
48 				  unsigned reg, unsigned writeval,
49 				  unsigned *readval)
50 {
51 	struct st_sensor_data *sdata = iio_priv(indio_dev);
52 	u8 readdata;
53 	int err;
54 
55 	if (!readval)
56 		return sdata->tf->write_byte(&sdata->tb, sdata->dev,
57 					     (u8)reg, (u8)writeval);
58 
59 	err = sdata->tf->read_byte(&sdata->tb, sdata->dev, (u8)reg, &readdata);
60 	if (err < 0)
61 		return err;
62 
63 	*readval = (unsigned)readdata;
64 
65 	return 0;
66 }
67 EXPORT_SYMBOL(st_sensors_debugfs_reg_access);
68 
st_sensors_match_odr(struct st_sensor_settings * sensor_settings,unsigned int odr,struct st_sensor_odr_avl * odr_out)69 static int st_sensors_match_odr(struct st_sensor_settings *sensor_settings,
70 			unsigned int odr, struct st_sensor_odr_avl *odr_out)
71 {
72 	int i, ret = -EINVAL;
73 
74 	for (i = 0; i < ST_SENSORS_ODR_LIST_MAX; i++) {
75 		if (sensor_settings->odr.odr_avl[i].hz == 0)
76 			goto st_sensors_match_odr_error;
77 
78 		if (sensor_settings->odr.odr_avl[i].hz == odr) {
79 			odr_out->hz = sensor_settings->odr.odr_avl[i].hz;
80 			odr_out->value = sensor_settings->odr.odr_avl[i].value;
81 			ret = 0;
82 			break;
83 		}
84 	}
85 
86 st_sensors_match_odr_error:
87 	return ret;
88 }
89 
st_sensors_set_odr(struct iio_dev * indio_dev,unsigned int odr)90 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr)
91 {
92 	int err;
93 	struct st_sensor_odr_avl odr_out = {0, 0};
94 	struct st_sensor_data *sdata = iio_priv(indio_dev);
95 
96 	if (!sdata->sensor_settings->odr.mask)
97 		return 0;
98 
99 	err = st_sensors_match_odr(sdata->sensor_settings, odr, &odr_out);
100 	if (err < 0)
101 		goto st_sensors_match_odr_error;
102 
103 	if ((sdata->sensor_settings->odr.addr ==
104 					sdata->sensor_settings->pw.addr) &&
105 				(sdata->sensor_settings->odr.mask ==
106 					sdata->sensor_settings->pw.mask)) {
107 		if (sdata->enabled == true) {
108 			err = st_sensors_write_data_with_mask(indio_dev,
109 				sdata->sensor_settings->odr.addr,
110 				sdata->sensor_settings->odr.mask,
111 				odr_out.value);
112 		} else {
113 			err = 0;
114 		}
115 	} else {
116 		err = st_sensors_write_data_with_mask(indio_dev,
117 			sdata->sensor_settings->odr.addr,
118 			sdata->sensor_settings->odr.mask,
119 			odr_out.value);
120 	}
121 	if (err >= 0)
122 		sdata->odr = odr_out.hz;
123 
124 st_sensors_match_odr_error:
125 	return err;
126 }
127 EXPORT_SYMBOL(st_sensors_set_odr);
128 
st_sensors_match_fs(struct st_sensor_settings * sensor_settings,unsigned int fs,int * index_fs_avl)129 static int st_sensors_match_fs(struct st_sensor_settings *sensor_settings,
130 					unsigned int fs, int *index_fs_avl)
131 {
132 	int i, ret = -EINVAL;
133 
134 	for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
135 		if (sensor_settings->fs.fs_avl[i].num == 0)
136 			goto st_sensors_match_odr_error;
137 
138 		if (sensor_settings->fs.fs_avl[i].num == fs) {
139 			*index_fs_avl = i;
140 			ret = 0;
141 			break;
142 		}
143 	}
144 
145 st_sensors_match_odr_error:
146 	return ret;
147 }
148 
st_sensors_set_fullscale(struct iio_dev * indio_dev,unsigned int fs)149 static int st_sensors_set_fullscale(struct iio_dev *indio_dev, unsigned int fs)
150 {
151 	int err, i = 0;
152 	struct st_sensor_data *sdata = iio_priv(indio_dev);
153 
154 	if (sdata->sensor_settings->fs.addr == 0)
155 		return 0;
156 
157 	err = st_sensors_match_fs(sdata->sensor_settings, fs, &i);
158 	if (err < 0)
159 		goto st_accel_set_fullscale_error;
160 
161 	err = st_sensors_write_data_with_mask(indio_dev,
162 				sdata->sensor_settings->fs.addr,
163 				sdata->sensor_settings->fs.mask,
164 				sdata->sensor_settings->fs.fs_avl[i].value);
165 	if (err < 0)
166 		goto st_accel_set_fullscale_error;
167 
168 	sdata->current_fullscale = (struct st_sensor_fullscale_avl *)
169 					&sdata->sensor_settings->fs.fs_avl[i];
170 	return err;
171 
172 st_accel_set_fullscale_error:
173 	dev_err(&indio_dev->dev, "failed to set new fullscale.\n");
174 	return err;
175 }
176 
st_sensors_set_enable(struct iio_dev * indio_dev,bool enable)177 int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable)
178 {
179 	u8 tmp_value;
180 	int err = -EINVAL;
181 	bool found = false;
182 	struct st_sensor_odr_avl odr_out = {0, 0};
183 	struct st_sensor_data *sdata = iio_priv(indio_dev);
184 
185 	if (enable) {
186 		tmp_value = sdata->sensor_settings->pw.value_on;
187 		if ((sdata->sensor_settings->odr.addr ==
188 					sdata->sensor_settings->pw.addr) &&
189 				(sdata->sensor_settings->odr.mask ==
190 					sdata->sensor_settings->pw.mask)) {
191 			err = st_sensors_match_odr(sdata->sensor_settings,
192 							sdata->odr, &odr_out);
193 			if (err < 0)
194 				goto set_enable_error;
195 			tmp_value = odr_out.value;
196 			found = true;
197 		}
198 		err = st_sensors_write_data_with_mask(indio_dev,
199 				sdata->sensor_settings->pw.addr,
200 				sdata->sensor_settings->pw.mask, tmp_value);
201 		if (err < 0)
202 			goto set_enable_error;
203 
204 		sdata->enabled = true;
205 
206 		if (found)
207 			sdata->odr = odr_out.hz;
208 	} else {
209 		err = st_sensors_write_data_with_mask(indio_dev,
210 				sdata->sensor_settings->pw.addr,
211 				sdata->sensor_settings->pw.mask,
212 				sdata->sensor_settings->pw.value_off);
213 		if (err < 0)
214 			goto set_enable_error;
215 
216 		sdata->enabled = false;
217 	}
218 
219 set_enable_error:
220 	return err;
221 }
222 EXPORT_SYMBOL(st_sensors_set_enable);
223 
st_sensors_set_axis_enable(struct iio_dev * indio_dev,u8 axis_enable)224 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable)
225 {
226 	struct st_sensor_data *sdata = iio_priv(indio_dev);
227 	int err = 0;
228 
229 	if (sdata->sensor_settings->enable_axis.addr)
230 		err = st_sensors_write_data_with_mask(indio_dev,
231 				sdata->sensor_settings->enable_axis.addr,
232 				sdata->sensor_settings->enable_axis.mask,
233 				axis_enable);
234 	return err;
235 }
236 EXPORT_SYMBOL(st_sensors_set_axis_enable);
237 
st_sensors_power_enable(struct iio_dev * indio_dev)238 int st_sensors_power_enable(struct iio_dev *indio_dev)
239 {
240 	struct st_sensor_data *pdata = iio_priv(indio_dev);
241 	int err;
242 
243 	/* Regulators not mandatory, but if requested we should enable them. */
244 	pdata->vdd = devm_regulator_get(indio_dev->dev.parent, "vdd");
245 	if (IS_ERR(pdata->vdd)) {
246 		dev_err(&indio_dev->dev, "unable to get Vdd supply\n");
247 		return PTR_ERR(pdata->vdd);
248 	}
249 	err = regulator_enable(pdata->vdd);
250 	if (err != 0) {
251 		dev_warn(&indio_dev->dev,
252 			 "Failed to enable specified Vdd supply\n");
253 		return err;
254 	}
255 
256 	pdata->vdd_io = devm_regulator_get(indio_dev->dev.parent, "vddio");
257 	if (IS_ERR(pdata->vdd_io)) {
258 		dev_err(&indio_dev->dev, "unable to get Vdd_IO supply\n");
259 		err = PTR_ERR(pdata->vdd_io);
260 		goto st_sensors_disable_vdd;
261 	}
262 	err = regulator_enable(pdata->vdd_io);
263 	if (err != 0) {
264 		dev_warn(&indio_dev->dev,
265 			 "Failed to enable specified Vdd_IO supply\n");
266 		goto st_sensors_disable_vdd;
267 	}
268 
269 	return 0;
270 
271 st_sensors_disable_vdd:
272 	regulator_disable(pdata->vdd);
273 	return err;
274 }
275 EXPORT_SYMBOL(st_sensors_power_enable);
276 
st_sensors_power_disable(struct iio_dev * indio_dev)277 void st_sensors_power_disable(struct iio_dev *indio_dev)
278 {
279 	struct st_sensor_data *pdata = iio_priv(indio_dev);
280 
281 	regulator_disable(pdata->vdd);
282 	regulator_disable(pdata->vdd_io);
283 }
284 EXPORT_SYMBOL(st_sensors_power_disable);
285 
st_sensors_set_drdy_int_pin(struct iio_dev * indio_dev,struct st_sensors_platform_data * pdata)286 static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev,
287 					struct st_sensors_platform_data *pdata)
288 {
289 	struct st_sensor_data *sdata = iio_priv(indio_dev);
290 
291 	/* Sensor does not support interrupts */
292 	if (!sdata->sensor_settings->drdy_irq.int1.addr &&
293 	    !sdata->sensor_settings->drdy_irq.int2.addr) {
294 		if (pdata->drdy_int_pin)
295 			dev_info(&indio_dev->dev,
296 				 "DRDY on pin INT%d specified, but sensor "
297 				 "does not support interrupts\n",
298 				 pdata->drdy_int_pin);
299 		return 0;
300 	}
301 
302 	switch (pdata->drdy_int_pin) {
303 	case 1:
304 		if (!sdata->sensor_settings->drdy_irq.int1.mask) {
305 			dev_err(&indio_dev->dev,
306 					"DRDY on INT1 not available.\n");
307 			return -EINVAL;
308 		}
309 		sdata->drdy_int_pin = 1;
310 		break;
311 	case 2:
312 		if (!sdata->sensor_settings->drdy_irq.int2.mask) {
313 			dev_err(&indio_dev->dev,
314 					"DRDY on INT2 not available.\n");
315 			return -EINVAL;
316 		}
317 		sdata->drdy_int_pin = 2;
318 		break;
319 	default:
320 		dev_err(&indio_dev->dev, "DRDY on pdata not valid.\n");
321 		return -EINVAL;
322 	}
323 
324 	if (pdata->open_drain) {
325 		if (!sdata->sensor_settings->drdy_irq.int1.addr_od &&
326 		    !sdata->sensor_settings->drdy_irq.int2.addr_od)
327 			dev_err(&indio_dev->dev,
328 				"open drain requested but unsupported.\n");
329 		else
330 			sdata->int_pin_open_drain = true;
331 	}
332 
333 	return 0;
334 }
335 
336 #ifdef CONFIG_OF
st_sensors_of_probe(struct device * dev,struct st_sensors_platform_data * defdata)337 static struct st_sensors_platform_data *st_sensors_of_probe(struct device *dev,
338 		struct st_sensors_platform_data *defdata)
339 {
340 	struct st_sensors_platform_data *pdata;
341 	struct device_node *np = dev->of_node;
342 	u32 val;
343 
344 	if (!np)
345 		return NULL;
346 
347 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
348 	if (!of_property_read_u32(np, "st,drdy-int-pin", &val) && (val <= 2))
349 		pdata->drdy_int_pin = (u8) val;
350 	else
351 		pdata->drdy_int_pin = defdata ? defdata->drdy_int_pin : 0;
352 
353 	pdata->open_drain = of_property_read_bool(np, "drive-open-drain");
354 
355 	return pdata;
356 }
357 
358 /**
359  * st_sensors_of_name_probe() - device tree probe for ST sensor name
360  * @dev: driver model representation of the device.
361  * @match: the OF match table for the device, containing compatible strings
362  *	but also a .data field with the corresponding internal kernel name
363  *	used by this sensor.
364  * @name: device name buffer reference.
365  * @len: device name buffer length.
366  *
367  * In effect this function matches a compatible string to an internal kernel
368  * name for a certain sensor device, so that the rest of the autodetection can
369  * rely on that name from this point on. I2C/SPI devices will be renamed
370  * to match the internal kernel convention.
371  */
st_sensors_of_name_probe(struct device * dev,const struct of_device_id * match,char * name,int len)372 void st_sensors_of_name_probe(struct device *dev,
373 			      const struct of_device_id *match,
374 			      char *name, int len)
375 {
376 	const struct of_device_id *of_id;
377 
378 	of_id = of_match_device(match, dev);
379 	if (!of_id || !of_id->data)
380 		return;
381 
382 	/* The name from the OF match takes precedence if present */
383 	strlcpy(name, of_id->data, len);
384 }
385 EXPORT_SYMBOL(st_sensors_of_name_probe);
386 #else
st_sensors_of_probe(struct device * dev,struct st_sensors_platform_data * defdata)387 static struct st_sensors_platform_data *st_sensors_of_probe(struct device *dev,
388 		struct st_sensors_platform_data *defdata)
389 {
390 	return NULL;
391 }
392 #endif
393 
st_sensors_init_sensor(struct iio_dev * indio_dev,struct st_sensors_platform_data * pdata)394 int st_sensors_init_sensor(struct iio_dev *indio_dev,
395 					struct st_sensors_platform_data *pdata)
396 {
397 	struct st_sensor_data *sdata = iio_priv(indio_dev);
398 	struct st_sensors_platform_data *of_pdata;
399 	int err = 0;
400 
401 	/* If OF/DT pdata exists, it will take precedence of anything else */
402 	of_pdata = st_sensors_of_probe(indio_dev->dev.parent, pdata);
403 	if (of_pdata)
404 		pdata = of_pdata;
405 
406 	if (pdata) {
407 		err = st_sensors_set_drdy_int_pin(indio_dev, pdata);
408 		if (err < 0)
409 			return err;
410 	}
411 
412 	err = st_sensors_set_enable(indio_dev, false);
413 	if (err < 0)
414 		return err;
415 
416 	/* Disable DRDY, this might be still be enabled after reboot. */
417 	err = st_sensors_set_dataready_irq(indio_dev, false);
418 	if (err < 0)
419 		return err;
420 
421 	if (sdata->current_fullscale) {
422 		err = st_sensors_set_fullscale(indio_dev,
423 						sdata->current_fullscale->num);
424 		if (err < 0)
425 			return err;
426 	} else
427 		dev_info(&indio_dev->dev, "Full-scale not possible\n");
428 
429 	err = st_sensors_set_odr(indio_dev, sdata->odr);
430 	if (err < 0)
431 		return err;
432 
433 	/* set BDU */
434 	if (sdata->sensor_settings->bdu.addr) {
435 		err = st_sensors_write_data_with_mask(indio_dev,
436 					sdata->sensor_settings->bdu.addr,
437 					sdata->sensor_settings->bdu.mask, true);
438 		if (err < 0)
439 			return err;
440 	}
441 
442 	/* set DAS */
443 	if (sdata->sensor_settings->das.addr) {
444 		err = st_sensors_write_data_with_mask(indio_dev,
445 					sdata->sensor_settings->das.addr,
446 					sdata->sensor_settings->das.mask, 1);
447 		if (err < 0)
448 			return err;
449 	}
450 
451 	if (sdata->int_pin_open_drain) {
452 		u8 addr, mask;
453 
454 		if (sdata->drdy_int_pin == 1) {
455 			addr = sdata->sensor_settings->drdy_irq.int1.addr_od;
456 			mask = sdata->sensor_settings->drdy_irq.int1.mask_od;
457 		} else {
458 			addr = sdata->sensor_settings->drdy_irq.int2.addr_od;
459 			mask = sdata->sensor_settings->drdy_irq.int2.mask_od;
460 		}
461 
462 		dev_info(&indio_dev->dev,
463 			 "set interrupt line to open drain mode on pin %d\n",
464 			 sdata->drdy_int_pin);
465 		err = st_sensors_write_data_with_mask(indio_dev, addr,
466 						      mask, 1);
467 		if (err < 0)
468 			return err;
469 	}
470 
471 	err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
472 
473 	return err;
474 }
475 EXPORT_SYMBOL(st_sensors_init_sensor);
476 
st_sensors_set_dataready_irq(struct iio_dev * indio_dev,bool enable)477 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable)
478 {
479 	int err;
480 	u8 drdy_addr, drdy_mask;
481 	struct st_sensor_data *sdata = iio_priv(indio_dev);
482 
483 	if (!sdata->sensor_settings->drdy_irq.int1.addr &&
484 	    !sdata->sensor_settings->drdy_irq.int2.addr) {
485 		/*
486 		 * there are some devices (e.g. LIS3MDL) where drdy line is
487 		 * routed to a given pin and it is not possible to select a
488 		 * different one. Take into account irq status register
489 		 * to understand if irq trigger can be properly supported
490 		 */
491 		if (sdata->sensor_settings->drdy_irq.stat_drdy.addr)
492 			sdata->hw_irq_trigger = enable;
493 		return 0;
494 	}
495 
496 	/* Enable/Disable the interrupt generator 1. */
497 	if (sdata->sensor_settings->drdy_irq.ig1.en_addr > 0) {
498 		err = st_sensors_write_data_with_mask(indio_dev,
499 				sdata->sensor_settings->drdy_irq.ig1.en_addr,
500 				sdata->sensor_settings->drdy_irq.ig1.en_mask,
501 				(int)enable);
502 		if (err < 0)
503 			goto st_accel_set_dataready_irq_error;
504 	}
505 
506 	if (sdata->drdy_int_pin == 1) {
507 		drdy_addr = sdata->sensor_settings->drdy_irq.int1.addr;
508 		drdy_mask = sdata->sensor_settings->drdy_irq.int1.mask;
509 	} else {
510 		drdy_addr = sdata->sensor_settings->drdy_irq.int2.addr;
511 		drdy_mask = sdata->sensor_settings->drdy_irq.int2.mask;
512 	}
513 
514 	/* Flag to the poll function that the hardware trigger is in use */
515 	sdata->hw_irq_trigger = enable;
516 
517 	/* Enable/Disable the interrupt generator for data ready. */
518 	err = st_sensors_write_data_with_mask(indio_dev, drdy_addr,
519 					      drdy_mask, (int)enable);
520 
521 st_accel_set_dataready_irq_error:
522 	return err;
523 }
524 EXPORT_SYMBOL(st_sensors_set_dataready_irq);
525 
st_sensors_set_fullscale_by_gain(struct iio_dev * indio_dev,int scale)526 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale)
527 {
528 	int err = -EINVAL, i;
529 	struct st_sensor_data *sdata = iio_priv(indio_dev);
530 
531 	for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
532 		if ((sdata->sensor_settings->fs.fs_avl[i].gain == scale) &&
533 				(sdata->sensor_settings->fs.fs_avl[i].gain != 0)) {
534 			err = 0;
535 			break;
536 		}
537 	}
538 	if (err < 0)
539 		goto st_sensors_match_scale_error;
540 
541 	err = st_sensors_set_fullscale(indio_dev,
542 				sdata->sensor_settings->fs.fs_avl[i].num);
543 
544 st_sensors_match_scale_error:
545 	return err;
546 }
547 EXPORT_SYMBOL(st_sensors_set_fullscale_by_gain);
548 
st_sensors_read_axis_data(struct iio_dev * indio_dev,struct iio_chan_spec const * ch,int * data)549 static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
550 				struct iio_chan_spec const *ch, int *data)
551 {
552 	int err;
553 	u8 *outdata;
554 	struct st_sensor_data *sdata = iio_priv(indio_dev);
555 	unsigned int byte_for_channel;
556 
557 	byte_for_channel = DIV_ROUND_UP(ch->scan_type.realbits +
558 					ch->scan_type.shift, 8);
559 	outdata = kmalloc(byte_for_channel, GFP_KERNEL);
560 	if (!outdata)
561 		return -ENOMEM;
562 
563 	err = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
564 				ch->address, byte_for_channel,
565 				outdata, sdata->multiread_bit);
566 	if (err < 0)
567 		goto st_sensors_free_memory;
568 
569 	if (byte_for_channel == 1)
570 		*data = (s8)*outdata;
571 	else if (byte_for_channel == 2)
572 		*data = (s16)get_unaligned_le16(outdata);
573 	else if (byte_for_channel == 3)
574 		*data = (s32)st_sensors_get_unaligned_le24(outdata);
575 
576 st_sensors_free_memory:
577 	kfree(outdata);
578 
579 	return err;
580 }
581 
st_sensors_read_info_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * ch,int * val)582 int st_sensors_read_info_raw(struct iio_dev *indio_dev,
583 				struct iio_chan_spec const *ch, int *val)
584 {
585 	int err;
586 	struct st_sensor_data *sdata = iio_priv(indio_dev);
587 
588 	mutex_lock(&indio_dev->mlock);
589 	if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
590 		err = -EBUSY;
591 		goto out;
592 	} else {
593 		err = st_sensors_set_enable(indio_dev, true);
594 		if (err < 0)
595 			goto out;
596 
597 		msleep((sdata->sensor_settings->bootime * 1000) / sdata->odr);
598 		err = st_sensors_read_axis_data(indio_dev, ch, val);
599 		if (err < 0)
600 			goto out;
601 
602 		*val = *val >> ch->scan_type.shift;
603 
604 		err = st_sensors_set_enable(indio_dev, false);
605 	}
606 out:
607 	mutex_unlock(&indio_dev->mlock);
608 
609 	return err;
610 }
611 EXPORT_SYMBOL(st_sensors_read_info_raw);
612 
st_sensors_init_interface_mode(struct iio_dev * indio_dev,const struct st_sensor_settings * sensor_settings)613 static int st_sensors_init_interface_mode(struct iio_dev *indio_dev,
614 			const struct st_sensor_settings *sensor_settings)
615 {
616 	struct st_sensor_data *sdata = iio_priv(indio_dev);
617 	struct device_node *np = sdata->dev->of_node;
618 	struct st_sensors_platform_data *pdata;
619 
620 	pdata = (struct st_sensors_platform_data *)sdata->dev->platform_data;
621 	if (((np && of_property_read_bool(np, "spi-3wire")) ||
622 	     (pdata && pdata->spi_3wire)) && sensor_settings->sim.addr) {
623 		int err;
624 
625 		err = sdata->tf->write_byte(&sdata->tb, sdata->dev,
626 					    sensor_settings->sim.addr,
627 					    sensor_settings->sim.value);
628 		if (err < 0) {
629 			dev_err(&indio_dev->dev,
630 				"failed to init interface mode\n");
631 			return err;
632 		}
633 	}
634 
635 	return 0;
636 }
637 
st_sensors_check_device_support(struct iio_dev * indio_dev,int num_sensors_list,const struct st_sensor_settings * sensor_settings)638 int st_sensors_check_device_support(struct iio_dev *indio_dev,
639 			int num_sensors_list,
640 			const struct st_sensor_settings *sensor_settings)
641 {
642 	int i, n, err = 0;
643 	u8 wai;
644 	struct st_sensor_data *sdata = iio_priv(indio_dev);
645 
646 	for (i = 0; i < num_sensors_list; i++) {
647 		for (n = 0; n < ST_SENSORS_MAX_4WAI; n++) {
648 			if (strcmp(indio_dev->name,
649 				sensor_settings[i].sensors_supported[n]) == 0) {
650 				break;
651 			}
652 		}
653 		if (n < ST_SENSORS_MAX_4WAI)
654 			break;
655 	}
656 	if (i == num_sensors_list) {
657 		dev_err(&indio_dev->dev, "device name %s not recognized.\n",
658 							indio_dev->name);
659 		return -ENODEV;
660 	}
661 
662 	err = st_sensors_init_interface_mode(indio_dev, &sensor_settings[i]);
663 	if (err < 0)
664 		return err;
665 
666 	if (sensor_settings[i].wai_addr) {
667 		err = sdata->tf->read_byte(&sdata->tb, sdata->dev,
668 					   sensor_settings[i].wai_addr, &wai);
669 		if (err < 0) {
670 			dev_err(&indio_dev->dev,
671 				"failed to read Who-Am-I register.\n");
672 			return err;
673 		}
674 
675 		if (sensor_settings[i].wai != wai) {
676 			dev_err(&indio_dev->dev,
677 				"%s: WhoAmI mismatch (0x%x).\n",
678 				indio_dev->name, wai);
679 			return -EINVAL;
680 		}
681 	}
682 
683 	sdata->sensor_settings =
684 			(struct st_sensor_settings *)&sensor_settings[i];
685 
686 	return i;
687 }
688 EXPORT_SYMBOL(st_sensors_check_device_support);
689 
st_sensors_sysfs_sampling_frequency_avail(struct device * dev,struct device_attribute * attr,char * buf)690 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
691 				struct device_attribute *attr, char *buf)
692 {
693 	int i, len = 0;
694 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
695 	struct st_sensor_data *sdata = iio_priv(indio_dev);
696 
697 	mutex_lock(&indio_dev->mlock);
698 	for (i = 0; i < ST_SENSORS_ODR_LIST_MAX; i++) {
699 		if (sdata->sensor_settings->odr.odr_avl[i].hz == 0)
700 			break;
701 
702 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
703 				sdata->sensor_settings->odr.odr_avl[i].hz);
704 	}
705 	mutex_unlock(&indio_dev->mlock);
706 	buf[len - 1] = '\n';
707 
708 	return len;
709 }
710 EXPORT_SYMBOL(st_sensors_sysfs_sampling_frequency_avail);
711 
st_sensors_sysfs_scale_avail(struct device * dev,struct device_attribute * attr,char * buf)712 ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
713 				struct device_attribute *attr, char *buf)
714 {
715 	int i, len = 0, q, r;
716 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
717 	struct st_sensor_data *sdata = iio_priv(indio_dev);
718 
719 	mutex_lock(&indio_dev->mlock);
720 	for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
721 		if (sdata->sensor_settings->fs.fs_avl[i].num == 0)
722 			break;
723 
724 		q = sdata->sensor_settings->fs.fs_avl[i].gain / 1000000;
725 		r = sdata->sensor_settings->fs.fs_avl[i].gain % 1000000;
726 
727 		len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ", q, r);
728 	}
729 	mutex_unlock(&indio_dev->mlock);
730 	buf[len - 1] = '\n';
731 
732 	return len;
733 }
734 EXPORT_SYMBOL(st_sensors_sysfs_scale_avail);
735 
736 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
737 MODULE_DESCRIPTION("STMicroelectronics ST-sensors core");
738 MODULE_LICENSE("GPL v2");
739