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_NS(st_sensors_debugfs_reg_access, IIO_ST_SENSORS);
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_NS(st_sensors_set_odr, IIO_ST_SENSORS);
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_NS(st_sensors_set_enable, IIO_ST_SENSORS);
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_NS(st_sensors_set_axis_enable, IIO_ST_SENSORS);
221
st_reg_disable(void * reg)222 static void st_reg_disable(void *reg)
223 {
224 regulator_disable(reg);
225 }
226
st_sensors_power_enable(struct iio_dev * indio_dev)227 int st_sensors_power_enable(struct iio_dev *indio_dev)
228 {
229 struct st_sensor_data *pdata = iio_priv(indio_dev);
230 struct device *parent = indio_dev->dev.parent;
231 int err;
232
233 /* Regulators not mandatory, but if requested we should enable them. */
234 pdata->vdd = devm_regulator_get(parent, "vdd");
235 if (IS_ERR(pdata->vdd))
236 return dev_err_probe(&indio_dev->dev, PTR_ERR(pdata->vdd),
237 "unable to get Vdd supply\n");
238
239 err = regulator_enable(pdata->vdd);
240 if (err != 0) {
241 dev_warn(&indio_dev->dev,
242 "Failed to enable specified Vdd supply\n");
243 return err;
244 }
245
246 err = devm_add_action_or_reset(parent, st_reg_disable, pdata->vdd);
247 if (err)
248 return err;
249
250 pdata->vdd_io = devm_regulator_get(parent, "vddio");
251 if (IS_ERR(pdata->vdd_io))
252 return dev_err_probe(&indio_dev->dev, PTR_ERR(pdata->vdd_io),
253 "unable to get Vdd_IO supply\n");
254
255 err = regulator_enable(pdata->vdd_io);
256 if (err != 0) {
257 dev_warn(&indio_dev->dev,
258 "Failed to enable specified Vdd_IO supply\n");
259 return err;
260 }
261
262 return devm_add_action_or_reset(parent, st_reg_disable, pdata->vdd_io);
263 }
264 EXPORT_SYMBOL_NS(st_sensors_power_enable, IIO_ST_SENSORS);
265
st_sensors_set_drdy_int_pin(struct iio_dev * indio_dev,struct st_sensors_platform_data * pdata)266 static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev,
267 struct st_sensors_platform_data *pdata)
268 {
269 struct st_sensor_data *sdata = iio_priv(indio_dev);
270
271 /* Sensor does not support interrupts */
272 if (!sdata->sensor_settings->drdy_irq.int1.addr &&
273 !sdata->sensor_settings->drdy_irq.int2.addr) {
274 if (pdata->drdy_int_pin)
275 dev_info(&indio_dev->dev,
276 "DRDY on pin INT%d specified, but sensor does not support interrupts\n",
277 pdata->drdy_int_pin);
278 return 0;
279 }
280
281 switch (pdata->drdy_int_pin) {
282 case 1:
283 if (!sdata->sensor_settings->drdy_irq.int1.mask) {
284 dev_err(&indio_dev->dev,
285 "DRDY on INT1 not available.\n");
286 return -EINVAL;
287 }
288 sdata->drdy_int_pin = 1;
289 break;
290 case 2:
291 if (!sdata->sensor_settings->drdy_irq.int2.mask) {
292 dev_err(&indio_dev->dev,
293 "DRDY on INT2 not available.\n");
294 return -EINVAL;
295 }
296 sdata->drdy_int_pin = 2;
297 break;
298 default:
299 dev_err(&indio_dev->dev, "DRDY on pdata not valid.\n");
300 return -EINVAL;
301 }
302
303 if (pdata->open_drain) {
304 if (!sdata->sensor_settings->drdy_irq.int1.addr_od &&
305 !sdata->sensor_settings->drdy_irq.int2.addr_od)
306 dev_err(&indio_dev->dev,
307 "open drain requested but unsupported.\n");
308 else
309 sdata->int_pin_open_drain = true;
310 }
311
312 return 0;
313 }
314
st_sensors_dev_probe(struct device * dev,struct st_sensors_platform_data * defdata)315 static struct st_sensors_platform_data *st_sensors_dev_probe(struct device *dev,
316 struct st_sensors_platform_data *defdata)
317 {
318 struct st_sensors_platform_data *pdata;
319 u32 val;
320
321 if (!dev_fwnode(dev))
322 return NULL;
323
324 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
325 if (!pdata)
326 return ERR_PTR(-ENOMEM);
327 if (!device_property_read_u32(dev, "st,drdy-int-pin", &val) && (val <= 2))
328 pdata->drdy_int_pin = (u8) val;
329 else
330 pdata->drdy_int_pin = defdata ? defdata->drdy_int_pin : 0;
331
332 pdata->open_drain = device_property_read_bool(dev, "drive-open-drain");
333
334 return pdata;
335 }
336
337 /**
338 * st_sensors_dev_name_probe() - device probe for ST sensor name
339 * @dev: driver model representation of the device.
340 * @name: device name buffer reference.
341 * @len: device name buffer length.
342 *
343 * In effect this function matches an ID to an internal kernel
344 * name for a certain sensor device, so that the rest of the autodetection can
345 * rely on that name from this point on. I2C/SPI devices will be renamed
346 * to match the internal kernel convention.
347 */
st_sensors_dev_name_probe(struct device * dev,char * name,int len)348 void st_sensors_dev_name_probe(struct device *dev, char *name, int len)
349 {
350 const void *match;
351
352 match = device_get_match_data(dev);
353 if (!match)
354 return;
355
356 /* The name from the match takes precedence if present */
357 strscpy(name, match, len);
358 }
359 EXPORT_SYMBOL_NS(st_sensors_dev_name_probe, IIO_ST_SENSORS);
360
st_sensors_init_sensor(struct iio_dev * indio_dev,struct st_sensors_platform_data * pdata)361 int st_sensors_init_sensor(struct iio_dev *indio_dev,
362 struct st_sensors_platform_data *pdata)
363 {
364 struct st_sensor_data *sdata = iio_priv(indio_dev);
365 struct st_sensors_platform_data *of_pdata;
366 int err = 0;
367
368 mutex_init(&sdata->odr_lock);
369
370 /* If OF/DT pdata exists, it will take precedence of anything else */
371 of_pdata = st_sensors_dev_probe(indio_dev->dev.parent, pdata);
372 if (IS_ERR(of_pdata))
373 return PTR_ERR(of_pdata);
374 if (of_pdata)
375 pdata = of_pdata;
376
377 if (pdata) {
378 err = st_sensors_set_drdy_int_pin(indio_dev, pdata);
379 if (err < 0)
380 return err;
381 }
382
383 err = st_sensors_set_enable(indio_dev, false);
384 if (err < 0)
385 return err;
386
387 /* Disable DRDY, this might be still be enabled after reboot. */
388 err = st_sensors_set_dataready_irq(indio_dev, false);
389 if (err < 0)
390 return err;
391
392 if (sdata->current_fullscale) {
393 err = st_sensors_set_fullscale(indio_dev,
394 sdata->current_fullscale->num);
395 if (err < 0)
396 return err;
397 } else
398 dev_info(&indio_dev->dev, "Full-scale not possible\n");
399
400 err = st_sensors_set_odr(indio_dev, sdata->odr);
401 if (err < 0)
402 return err;
403
404 /* set BDU */
405 if (sdata->sensor_settings->bdu.addr) {
406 err = st_sensors_write_data_with_mask(indio_dev,
407 sdata->sensor_settings->bdu.addr,
408 sdata->sensor_settings->bdu.mask, true);
409 if (err < 0)
410 return err;
411 }
412
413 /* set DAS */
414 if (sdata->sensor_settings->das.addr) {
415 err = st_sensors_write_data_with_mask(indio_dev,
416 sdata->sensor_settings->das.addr,
417 sdata->sensor_settings->das.mask, 1);
418 if (err < 0)
419 return err;
420 }
421
422 if (sdata->int_pin_open_drain) {
423 u8 addr, mask;
424
425 if (sdata->drdy_int_pin == 1) {
426 addr = sdata->sensor_settings->drdy_irq.int1.addr_od;
427 mask = sdata->sensor_settings->drdy_irq.int1.mask_od;
428 } else {
429 addr = sdata->sensor_settings->drdy_irq.int2.addr_od;
430 mask = sdata->sensor_settings->drdy_irq.int2.mask_od;
431 }
432
433 dev_info(&indio_dev->dev,
434 "set interrupt line to open drain mode on pin %d\n",
435 sdata->drdy_int_pin);
436 err = st_sensors_write_data_with_mask(indio_dev, addr,
437 mask, 1);
438 if (err < 0)
439 return err;
440 }
441
442 err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
443
444 return err;
445 }
446 EXPORT_SYMBOL_NS(st_sensors_init_sensor, IIO_ST_SENSORS);
447
st_sensors_set_dataready_irq(struct iio_dev * indio_dev,bool enable)448 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable)
449 {
450 int err;
451 u8 drdy_addr, drdy_mask;
452 struct st_sensor_data *sdata = iio_priv(indio_dev);
453
454 if (!sdata->sensor_settings->drdy_irq.int1.addr &&
455 !sdata->sensor_settings->drdy_irq.int2.addr) {
456 /*
457 * there are some devices (e.g. LIS3MDL) where drdy line is
458 * routed to a given pin and it is not possible to select a
459 * different one. Take into account irq status register
460 * to understand if irq trigger can be properly supported
461 */
462 if (sdata->sensor_settings->drdy_irq.stat_drdy.addr)
463 sdata->hw_irq_trigger = enable;
464 return 0;
465 }
466
467 /* Enable/Disable the interrupt generator 1. */
468 if (sdata->sensor_settings->drdy_irq.ig1.en_addr > 0) {
469 err = st_sensors_write_data_with_mask(indio_dev,
470 sdata->sensor_settings->drdy_irq.ig1.en_addr,
471 sdata->sensor_settings->drdy_irq.ig1.en_mask,
472 (int)enable);
473 if (err < 0)
474 goto st_accel_set_dataready_irq_error;
475 }
476
477 if (sdata->drdy_int_pin == 1) {
478 drdy_addr = sdata->sensor_settings->drdy_irq.int1.addr;
479 drdy_mask = sdata->sensor_settings->drdy_irq.int1.mask;
480 } else {
481 drdy_addr = sdata->sensor_settings->drdy_irq.int2.addr;
482 drdy_mask = sdata->sensor_settings->drdy_irq.int2.mask;
483 }
484
485 /* Flag to the poll function that the hardware trigger is in use */
486 sdata->hw_irq_trigger = enable;
487
488 /* Enable/Disable the interrupt generator for data ready. */
489 err = st_sensors_write_data_with_mask(indio_dev, drdy_addr,
490 drdy_mask, (int)enable);
491
492 st_accel_set_dataready_irq_error:
493 return err;
494 }
495 EXPORT_SYMBOL_NS(st_sensors_set_dataready_irq, IIO_ST_SENSORS);
496
st_sensors_set_fullscale_by_gain(struct iio_dev * indio_dev,int scale)497 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale)
498 {
499 int err = -EINVAL, i;
500 struct st_sensor_data *sdata = iio_priv(indio_dev);
501
502 for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
503 if ((sdata->sensor_settings->fs.fs_avl[i].gain == scale) &&
504 (sdata->sensor_settings->fs.fs_avl[i].gain != 0)) {
505 err = 0;
506 break;
507 }
508 }
509 if (err < 0)
510 goto st_sensors_match_scale_error;
511
512 err = st_sensors_set_fullscale(indio_dev,
513 sdata->sensor_settings->fs.fs_avl[i].num);
514
515 st_sensors_match_scale_error:
516 return err;
517 }
518 EXPORT_SYMBOL_NS(st_sensors_set_fullscale_by_gain, IIO_ST_SENSORS);
519
st_sensors_read_axis_data(struct iio_dev * indio_dev,struct iio_chan_spec const * ch,int * data)520 static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
521 struct iio_chan_spec const *ch, int *data)
522 {
523 int err;
524 u8 *outdata;
525 struct st_sensor_data *sdata = iio_priv(indio_dev);
526 unsigned int byte_for_channel;
527
528 byte_for_channel = DIV_ROUND_UP(ch->scan_type.realbits +
529 ch->scan_type.shift, 8);
530 outdata = kmalloc(byte_for_channel, GFP_DMA | GFP_KERNEL);
531 if (!outdata)
532 return -ENOMEM;
533
534 err = regmap_bulk_read(sdata->regmap, ch->address,
535 outdata, byte_for_channel);
536 if (err < 0)
537 goto st_sensors_free_memory;
538
539 if (byte_for_channel == 1)
540 *data = (s8)*outdata;
541 else if (byte_for_channel == 2)
542 *data = (s16)get_unaligned_le16(outdata);
543 else if (byte_for_channel == 3)
544 *data = (s32)sign_extend32(get_unaligned_le24(outdata), 23);
545
546 st_sensors_free_memory:
547 kfree(outdata);
548
549 return err;
550 }
551
st_sensors_read_info_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * ch,int * val)552 int st_sensors_read_info_raw(struct iio_dev *indio_dev,
553 struct iio_chan_spec const *ch, int *val)
554 {
555 int err;
556 struct st_sensor_data *sdata = iio_priv(indio_dev);
557
558 err = iio_device_claim_direct_mode(indio_dev);
559 if (err)
560 return err;
561
562 mutex_lock(&sdata->odr_lock);
563
564 err = st_sensors_set_enable(indio_dev, true);
565 if (err < 0)
566 goto out;
567
568 msleep((sdata->sensor_settings->bootime * 1000) / sdata->odr);
569 err = st_sensors_read_axis_data(indio_dev, ch, val);
570 if (err < 0)
571 goto out;
572
573 *val = *val >> ch->scan_type.shift;
574
575 err = st_sensors_set_enable(indio_dev, false);
576
577 out:
578 mutex_unlock(&sdata->odr_lock);
579 iio_device_release_direct_mode(indio_dev);
580
581 return err;
582 }
583 EXPORT_SYMBOL_NS(st_sensors_read_info_raw, IIO_ST_SENSORS);
584
585 /*
586 * st_sensors_get_settings_index() - get index of the sensor settings for a
587 * specific device from list of settings
588 * @name: device name buffer reference.
589 * @list: sensor settings list.
590 * @list_length: length of sensor settings list.
591 *
592 * Return: non negative number on success (valid index),
593 * negative error code otherwise.
594 */
st_sensors_get_settings_index(const char * name,const struct st_sensor_settings * list,const int list_length)595 int st_sensors_get_settings_index(const char *name,
596 const struct st_sensor_settings *list,
597 const int list_length)
598 {
599 int i, n;
600
601 for (i = 0; i < list_length; i++) {
602 for (n = 0; n < ST_SENSORS_MAX_4WAI; n++) {
603 if (strcmp(name, list[i].sensors_supported[n]) == 0)
604 return i;
605 }
606 }
607
608 return -ENODEV;
609 }
610 EXPORT_SYMBOL_NS(st_sensors_get_settings_index, IIO_ST_SENSORS);
611
612 /*
613 * st_sensors_verify_id() - verify sensor ID (WhoAmI) is matching with the
614 * expected value
615 * @indio_dev: IIO device reference.
616 *
617 * Return: 0 on success (valid sensor ID), else a negative error code.
618 */
st_sensors_verify_id(struct iio_dev * indio_dev)619 int st_sensors_verify_id(struct iio_dev *indio_dev)
620 {
621 struct st_sensor_data *sdata = iio_priv(indio_dev);
622 int wai, err;
623
624 if (sdata->sensor_settings->wai_addr) {
625 err = regmap_read(sdata->regmap,
626 sdata->sensor_settings->wai_addr, &wai);
627 if (err < 0) {
628 dev_err(&indio_dev->dev,
629 "failed to read Who-Am-I register.\n");
630 return err;
631 }
632
633 if (sdata->sensor_settings->wai != wai) {
634 dev_err(&indio_dev->dev,
635 "%s: WhoAmI mismatch (0x%x).\n",
636 indio_dev->name, wai);
637 return -EINVAL;
638 }
639 }
640
641 return 0;
642 }
643 EXPORT_SYMBOL_NS(st_sensors_verify_id, IIO_ST_SENSORS);
644
st_sensors_sysfs_sampling_frequency_avail(struct device * dev,struct device_attribute * attr,char * buf)645 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
646 struct device_attribute *attr, char *buf)
647 {
648 int i, len = 0;
649 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
650 struct st_sensor_data *sdata = iio_priv(indio_dev);
651
652 for (i = 0; i < ST_SENSORS_ODR_LIST_MAX; i++) {
653 if (sdata->sensor_settings->odr.odr_avl[i].hz == 0)
654 break;
655
656 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
657 sdata->sensor_settings->odr.odr_avl[i].hz);
658 }
659 buf[len - 1] = '\n';
660
661 return len;
662 }
663 EXPORT_SYMBOL_NS(st_sensors_sysfs_sampling_frequency_avail, IIO_ST_SENSORS);
664
st_sensors_sysfs_scale_avail(struct device * dev,struct device_attribute * attr,char * buf)665 ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
666 struct device_attribute *attr, char *buf)
667 {
668 int i, len = 0, q, r;
669 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
670 struct st_sensor_data *sdata = iio_priv(indio_dev);
671
672 for (i = 0; i < ST_SENSORS_FULLSCALE_AVL_MAX; i++) {
673 if (sdata->sensor_settings->fs.fs_avl[i].num == 0)
674 break;
675
676 q = sdata->sensor_settings->fs.fs_avl[i].gain / 1000000;
677 r = sdata->sensor_settings->fs.fs_avl[i].gain % 1000000;
678
679 len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ", q, r);
680 }
681 buf[len - 1] = '\n';
682
683 return len;
684 }
685 EXPORT_SYMBOL_NS(st_sensors_sysfs_scale_avail, IIO_ST_SENSORS);
686
687 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
688 MODULE_DESCRIPTION("STMicroelectronics ST-sensors core");
689 MODULE_LICENSE("GPL v2");
690