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