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