2 * STMicroelectronics st_lsm6dsx i2c controller driver
4 * i2c controller embedded in lsm6dx series can connect up to four
5 * slave devices using accelerometer sensor as trigger for i2c
6 * read/write operations. Current implementation relies on SLV0 channel
7 * for slave configuration and SLV{1,2,3} to read data and push them into
10 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
12 * Permission to use, copy, modify, and/or distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include <linux/module.h>
26 #include <linux/regmap.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/bitfield.h>
31 #include "st_lsm6dsx.h"
33 #define ST_LSM6DSX_SLV_ADDR(n, base) ((base) + (n) * 3)
34 #define ST_LSM6DSX_SLV_SUB_ADDR(n, base) ((base) + 1 + (n) * 3)
35 #define ST_LSM6DSX_SLV_CONFIG(n, base) ((base) + 2 + (n) * 3)
37 #define ST_LS6DSX_READ_OP_MASK GENMASK(2, 0)
39 static const struct st_lsm6dsx_ext_dev_settings st_lsm6dsx_ext_dev_table
[] = {
47 .id
= ST_LSM6DSX_ID_MAGN
,
51 .mask
= GENMASK(3, 2),
53 .odr_avl
[0] = { 10000, 0x0 },
54 .odr_avl
[1] = { 20000, 0x1 },
55 .odr_avl
[2] = { 50000, 0x2 },
56 .odr_avl
[3] = { 100000, 0x3 },
73 .mask
= GENMASK(1, 0),
93 static void st_lsm6dsx_shub_wait_complete(struct st_lsm6dsx_hw
*hw
)
95 struct st_lsm6dsx_sensor
*sensor
;
98 sensor
= iio_priv(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
]);
99 odr
= (hw
->enable_mask
& BIT(ST_LSM6DSX_ID_ACC
)) ? sensor
->odr
: 12500;
100 msleep((2000000U / odr
) + 1);
104 * st_lsm6dsx_shub_read_output - read i2c controller register
106 * Read st_lsm6dsx i2c controller register
109 st_lsm6dsx_shub_read_output(struct st_lsm6dsx_hw
*hw
, u8
*data
,
112 const struct st_lsm6dsx_shub_settings
*hub_settings
;
115 mutex_lock(&hw
->page_lock
);
117 hub_settings
= &hw
->settings
->shub_settings
;
118 if (hub_settings
->shub_out
.sec_page
) {
119 err
= st_lsm6dsx_set_page(hw
, true);
124 err
= regmap_bulk_read(hw
->regmap
, hub_settings
->shub_out
.addr
,
127 if (hub_settings
->shub_out
.sec_page
)
128 st_lsm6dsx_set_page(hw
, false);
130 mutex_unlock(&hw
->page_lock
);
136 * st_lsm6dsx_shub_write_reg - write i2c controller register
138 * Write st_lsm6dsx i2c controller register
140 static int st_lsm6dsx_shub_write_reg(struct st_lsm6dsx_hw
*hw
, u8 addr
,
145 mutex_lock(&hw
->page_lock
);
146 err
= st_lsm6dsx_set_page(hw
, true);
150 err
= regmap_bulk_write(hw
->regmap
, addr
, data
, len
);
152 st_lsm6dsx_set_page(hw
, false);
154 mutex_unlock(&hw
->page_lock
);
160 st_lsm6dsx_shub_write_reg_with_mask(struct st_lsm6dsx_hw
*hw
, u8 addr
,
165 mutex_lock(&hw
->page_lock
);
166 err
= st_lsm6dsx_set_page(hw
, true);
170 err
= regmap_update_bits(hw
->regmap
, addr
, mask
, val
);
172 st_lsm6dsx_set_page(hw
, false);
174 mutex_unlock(&hw
->page_lock
);
179 static int st_lsm6dsx_shub_master_enable(struct st_lsm6dsx_sensor
*sensor
,
182 const struct st_lsm6dsx_shub_settings
*hub_settings
;
183 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
187 /* enable acc sensor as trigger */
188 err
= st_lsm6dsx_sensor_set_enable(sensor
, enable
);
192 mutex_lock(&hw
->page_lock
);
194 hub_settings
= &hw
->settings
->shub_settings
;
195 if (hub_settings
->master_en
.sec_page
) {
196 err
= st_lsm6dsx_set_page(hw
, true);
201 data
= ST_LSM6DSX_SHIFT_VAL(enable
, hub_settings
->master_en
.mask
);
202 err
= regmap_update_bits(hw
->regmap
, hub_settings
->master_en
.addr
,
203 hub_settings
->master_en
.mask
, data
);
205 if (hub_settings
->master_en
.sec_page
)
206 st_lsm6dsx_set_page(hw
, false);
208 mutex_unlock(&hw
->page_lock
);
214 * st_lsm6dsx_shub_read - read data from slave device register
216 * Read data from slave device register. SLV0 is used for
217 * one-shot read operation
220 st_lsm6dsx_shub_read(struct st_lsm6dsx_sensor
*sensor
, u8 addr
,
223 const struct st_lsm6dsx_shub_settings
*hub_settings
;
224 u8 config
[3], slv_addr
, slv_config
= 0;
225 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
226 const struct st_lsm6dsx_reg
*aux_sens
;
229 hub_settings
= &hw
->settings
->shub_settings
;
230 slv_addr
= ST_LSM6DSX_SLV_ADDR(0, hub_settings
->slv0_addr
);
231 aux_sens
= &hw
->settings
->shub_settings
.aux_sens
;
232 /* do not overwrite aux_sens */
233 if (slv_addr
+ 2 == aux_sens
->addr
)
234 slv_config
= ST_LSM6DSX_SHIFT_VAL(3, aux_sens
->mask
);
236 config
[0] = (sensor
->ext_info
.addr
<< 1) | 1;
238 config
[2] = (len
& ST_LS6DSX_READ_OP_MASK
) | slv_config
;
240 err
= st_lsm6dsx_shub_write_reg(hw
, slv_addr
, config
,
245 err
= st_lsm6dsx_shub_master_enable(sensor
, true);
249 st_lsm6dsx_shub_wait_complete(hw
);
251 err
= st_lsm6dsx_shub_read_output(hw
, data
,
252 len
& ST_LS6DSX_READ_OP_MASK
);
254 st_lsm6dsx_shub_master_enable(sensor
, false);
256 config
[0] = hub_settings
->pause
;
258 config
[2] = slv_config
;
259 return st_lsm6dsx_shub_write_reg(hw
, slv_addr
, config
,
264 * st_lsm6dsx_shub_write - write data to slave device register
266 * Write data from slave device register. SLV0 is used for
267 * one-shot write operation
270 st_lsm6dsx_shub_write(struct st_lsm6dsx_sensor
*sensor
, u8 addr
,
273 const struct st_lsm6dsx_shub_settings
*hub_settings
;
274 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
275 u8 config
[2], slv_addr
;
278 hub_settings
= &hw
->settings
->shub_settings
;
279 if (hub_settings
->wr_once
.addr
) {
282 data
= ST_LSM6DSX_SHIFT_VAL(1, hub_settings
->wr_once
.mask
);
283 err
= st_lsm6dsx_shub_write_reg_with_mask(hw
,
284 hub_settings
->wr_once
.addr
,
285 hub_settings
->wr_once
.mask
,
291 slv_addr
= ST_LSM6DSX_SLV_ADDR(0, hub_settings
->slv0_addr
);
292 config
[0] = sensor
->ext_info
.addr
<< 1;
293 for (i
= 0 ; i
< len
; i
++) {
294 config
[1] = addr
+ i
;
296 err
= st_lsm6dsx_shub_write_reg(hw
, slv_addr
, config
,
301 err
= st_lsm6dsx_shub_write_reg(hw
, hub_settings
->dw_slv0_addr
,
306 err
= st_lsm6dsx_shub_master_enable(sensor
, true);
310 st_lsm6dsx_shub_wait_complete(hw
);
312 st_lsm6dsx_shub_master_enable(sensor
, false);
315 config
[0] = hub_settings
->pause
;
317 return st_lsm6dsx_shub_write_reg(hw
, slv_addr
, config
, sizeof(config
));
321 st_lsm6dsx_shub_write_with_mask(struct st_lsm6dsx_sensor
*sensor
,
322 u8 addr
, u8 mask
, u8 val
)
327 err
= st_lsm6dsx_shub_read(sensor
, addr
, &data
, sizeof(data
));
331 data
= ((data
& ~mask
) | (val
<< __ffs(mask
) & mask
));
333 return st_lsm6dsx_shub_write(sensor
, addr
, &data
, sizeof(data
));
337 st_lsm6dsx_shub_get_odr_val(struct st_lsm6dsx_sensor
*sensor
,
340 const struct st_lsm6dsx_ext_dev_settings
*settings
;
343 settings
= sensor
->ext_info
.settings
;
344 for (i
= 0; i
< settings
->odr_table
.odr_len
; i
++) {
345 if (settings
->odr_table
.odr_avl
[i
].milli_hz
== odr
)
349 if (i
== settings
->odr_table
.odr_len
)
352 *val
= settings
->odr_table
.odr_avl
[i
].val
;
357 st_lsm6dsx_shub_set_odr(struct st_lsm6dsx_sensor
*sensor
, u32 odr
)
359 const struct st_lsm6dsx_ext_dev_settings
*settings
;
363 err
= st_lsm6dsx_shub_get_odr_val(sensor
, odr
, &val
);
367 settings
= sensor
->ext_info
.settings
;
368 return st_lsm6dsx_shub_write_with_mask(sensor
,
369 settings
->odr_table
.reg
.addr
,
370 settings
->odr_table
.reg
.mask
,
374 /* use SLV{1,2,3} for FIFO read operations */
376 st_lsm6dsx_shub_config_channels(struct st_lsm6dsx_sensor
*sensor
,
379 const struct st_lsm6dsx_shub_settings
*hub_settings
;
380 const struct st_lsm6dsx_ext_dev_settings
*settings
;
381 u8 config
[9] = {}, enable_mask
, slv_addr
;
382 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
383 struct st_lsm6dsx_sensor
*cur_sensor
;
386 hub_settings
= &hw
->settings
->shub_settings
;
388 enable_mask
= hw
->enable_mask
| BIT(sensor
->id
);
390 enable_mask
= hw
->enable_mask
& ~BIT(sensor
->id
);
392 for (i
= ST_LSM6DSX_ID_EXT0
; i
<= ST_LSM6DSX_ID_EXT2
; i
++) {
393 if (!hw
->iio_devs
[i
])
396 cur_sensor
= iio_priv(hw
->iio_devs
[i
]);
397 if (!(enable_mask
& BIT(cur_sensor
->id
)))
400 settings
= cur_sensor
->ext_info
.settings
;
401 config
[j
] = (sensor
->ext_info
.addr
<< 1) | 1;
402 config
[j
+ 1] = settings
->out
.addr
;
403 config
[j
+ 2] = (settings
->out
.len
& ST_LS6DSX_READ_OP_MASK
) |
404 hub_settings
->batch_en
;
408 slv_addr
= ST_LSM6DSX_SLV_ADDR(1, hub_settings
->slv0_addr
);
409 return st_lsm6dsx_shub_write_reg(hw
, slv_addr
, config
,
413 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor
*sensor
, bool enable
)
415 const struct st_lsm6dsx_ext_dev_settings
*settings
;
418 err
= st_lsm6dsx_shub_config_channels(sensor
, enable
);
422 settings
= sensor
->ext_info
.settings
;
424 err
= st_lsm6dsx_shub_set_odr(sensor
, sensor
->odr
);
428 err
= st_lsm6dsx_shub_write_with_mask(sensor
,
429 settings
->odr_table
.reg
.addr
,
430 settings
->odr_table
.reg
.mask
, 0);
435 if (settings
->pwr_table
.reg
.addr
) {
438 val
= enable
? settings
->pwr_table
.on_val
439 : settings
->pwr_table
.off_val
;
440 err
= st_lsm6dsx_shub_write_with_mask(sensor
,
441 settings
->pwr_table
.reg
.addr
,
442 settings
->pwr_table
.reg
.mask
, val
);
447 return st_lsm6dsx_shub_master_enable(sensor
, enable
);
451 st_lsm6dsx_shub_read_oneshot(struct st_lsm6dsx_sensor
*sensor
,
452 struct iio_chan_spec
const *ch
,
458 err
= st_lsm6dsx_shub_set_enable(sensor
, true);
462 delay
= 1000000000 / sensor
->odr
;
463 usleep_range(delay
, 2 * delay
);
465 len
= min_t(int, sizeof(data
), ch
->scan_type
.realbits
>> 3);
466 err
= st_lsm6dsx_shub_read(sensor
, ch
->address
, data
, len
);
468 st_lsm6dsx_shub_set_enable(sensor
, false);
475 *val
= (s16
)le16_to_cpu(*((__le16
*)data
));
485 st_lsm6dsx_shub_read_raw(struct iio_dev
*iio_dev
,
486 struct iio_chan_spec
const *ch
,
487 int *val
, int *val2
, long mask
)
489 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
493 case IIO_CHAN_INFO_RAW
:
494 ret
= iio_device_claim_direct_mode(iio_dev
);
498 ret
= st_lsm6dsx_shub_read_oneshot(sensor
, ch
, val
);
499 iio_device_release_direct_mode(iio_dev
);
501 case IIO_CHAN_INFO_SAMP_FREQ
:
502 *val
= sensor
->odr
/ 1000;
503 *val2
= (sensor
->odr
% 1000) * 1000;
504 ret
= IIO_VAL_INT_PLUS_MICRO
;
506 case IIO_CHAN_INFO_SCALE
:
508 *val2
= sensor
->gain
;
509 ret
= IIO_VAL_INT_PLUS_MICRO
;
520 st_lsm6dsx_shub_write_raw(struct iio_dev
*iio_dev
,
521 struct iio_chan_spec
const *chan
,
522 int val
, int val2
, long mask
)
524 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
527 err
= iio_device_claim_direct_mode(iio_dev
);
532 case IIO_CHAN_INFO_SAMP_FREQ
: {
535 val
= val
* 1000 + val2
/ 1000;
536 err
= st_lsm6dsx_shub_get_odr_val(sensor
, val
, &data
);
546 iio_device_release_direct_mode(iio_dev
);
552 st_lsm6dsx_shub_sampling_freq_avail(struct device
*dev
,
553 struct device_attribute
*attr
,
556 struct st_lsm6dsx_sensor
*sensor
= iio_priv(dev_get_drvdata(dev
));
557 const struct st_lsm6dsx_ext_dev_settings
*settings
;
560 settings
= sensor
->ext_info
.settings
;
561 for (i
= 0; i
< settings
->odr_table
.odr_len
; i
++) {
562 u32 val
= settings
->odr_table
.odr_avl
[i
].milli_hz
;
564 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%03d ",
565 val
/ 1000, val
% 1000);
572 static ssize_t
st_lsm6dsx_shub_scale_avail(struct device
*dev
,
573 struct device_attribute
*attr
,
576 struct st_lsm6dsx_sensor
*sensor
= iio_priv(dev_get_drvdata(dev
));
577 const struct st_lsm6dsx_ext_dev_settings
*settings
;
580 settings
= sensor
->ext_info
.settings
;
581 for (i
= 0; i
< settings
->fs_table
.fs_len
; i
++)
582 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06u ",
583 settings
->fs_table
.fs_avl
[i
].gain
);
589 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_shub_sampling_freq_avail
);
590 static IIO_DEVICE_ATTR(in_scale_available
, 0444,
591 st_lsm6dsx_shub_scale_avail
, NULL
, 0);
592 static struct attribute
*st_lsm6dsx_ext_attributes
[] = {
593 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
594 &iio_dev_attr_in_scale_available
.dev_attr
.attr
,
598 static const struct attribute_group st_lsm6dsx_ext_attribute_group
= {
599 .attrs
= st_lsm6dsx_ext_attributes
,
602 static const struct iio_info st_lsm6dsx_ext_info
= {
603 .attrs
= &st_lsm6dsx_ext_attribute_group
,
604 .read_raw
= st_lsm6dsx_shub_read_raw
,
605 .write_raw
= st_lsm6dsx_shub_write_raw
,
606 .hwfifo_set_watermark
= st_lsm6dsx_set_watermark
,
609 static struct iio_dev
*
610 st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw
*hw
,
611 enum st_lsm6dsx_sensor_id id
,
612 const struct st_lsm6dsx_ext_dev_settings
*info
,
613 u8 i2c_addr
, const char *name
)
615 struct iio_chan_spec
*ext_channels
;
616 struct st_lsm6dsx_sensor
*sensor
;
617 struct iio_dev
*iio_dev
;
619 iio_dev
= devm_iio_device_alloc(hw
->dev
, sizeof(*sensor
));
623 iio_dev
->modes
= INDIO_DIRECT_MODE
;
624 iio_dev
->dev
.parent
= hw
->dev
;
625 iio_dev
->info
= &st_lsm6dsx_ext_info
;
627 sensor
= iio_priv(iio_dev
);
630 sensor
->odr
= info
->odr_table
.odr_avl
[0].milli_hz
;
631 sensor
->gain
= info
->fs_table
.fs_avl
[0].gain
;
632 sensor
->ext_info
.settings
= info
;
633 sensor
->ext_info
.addr
= i2c_addr
;
634 sensor
->watermark
= 1;
637 case ST_LSM6DSX_ID_MAGN
: {
638 const struct iio_chan_spec magn_channels
[] = {
639 ST_LSM6DSX_CHANNEL(IIO_MAGN
, info
->out
.addr
,
641 ST_LSM6DSX_CHANNEL(IIO_MAGN
, info
->out
.addr
+ 2,
643 ST_LSM6DSX_CHANNEL(IIO_MAGN
, info
->out
.addr
+ 4,
645 IIO_CHAN_SOFT_TIMESTAMP(3),
648 ext_channels
= devm_kzalloc(hw
->dev
, sizeof(magn_channels
),
653 memcpy(ext_channels
, magn_channels
, sizeof(magn_channels
));
654 iio_dev
->available_scan_masks
= st_lsm6dsx_available_scan_masks
;
655 iio_dev
->channels
= ext_channels
;
656 iio_dev
->num_channels
= ARRAY_SIZE(magn_channels
);
658 scnprintf(sensor
->name
, sizeof(sensor
->name
), "%s_magn",
665 iio_dev
->name
= sensor
->name
;
670 static int st_lsm6dsx_shub_init_device(struct st_lsm6dsx_sensor
*sensor
)
672 const struct st_lsm6dsx_ext_dev_settings
*settings
;
675 settings
= sensor
->ext_info
.settings
;
676 if (settings
->bdu
.addr
) {
677 err
= st_lsm6dsx_shub_write_with_mask(sensor
,
679 settings
->bdu
.mask
, 1);
684 if (settings
->temp_comp
.addr
) {
685 err
= st_lsm6dsx_shub_write_with_mask(sensor
,
686 settings
->temp_comp
.addr
,
687 settings
->temp_comp
.mask
, 1);
692 if (settings
->off_canc
.addr
) {
693 err
= st_lsm6dsx_shub_write_with_mask(sensor
,
694 settings
->off_canc
.addr
,
695 settings
->off_canc
.mask
, 1);
704 st_lsm6dsx_shub_check_wai(struct st_lsm6dsx_hw
*hw
, u8
*i2c_addr
,
705 const struct st_lsm6dsx_ext_dev_settings
*settings
)
707 const struct st_lsm6dsx_shub_settings
*hub_settings
;
708 u8 config
[3], data
, slv_addr
, slv_config
= 0;
709 const struct st_lsm6dsx_reg
*aux_sens
;
710 struct st_lsm6dsx_sensor
*sensor
;
714 sensor
= iio_priv(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
]);
715 hub_settings
= &hw
->settings
->shub_settings
;
716 aux_sens
= &hw
->settings
->shub_settings
.aux_sens
;
717 slv_addr
= ST_LSM6DSX_SLV_ADDR(0, hub_settings
->slv0_addr
);
718 /* do not overwrite aux_sens */
719 if (slv_addr
+ 2 == aux_sens
->addr
)
720 slv_config
= ST_LSM6DSX_SHIFT_VAL(3, aux_sens
->mask
);
722 for (i
= 0; i
< ARRAY_SIZE(settings
->i2c_addr
); i
++) {
723 if (!settings
->i2c_addr
[i
])
726 /* read wai slave register */
727 config
[0] = (settings
->i2c_addr
[i
] << 1) | 0x1;
728 config
[1] = settings
->wai
.addr
;
729 config
[2] = 0x1 | slv_config
;
731 err
= st_lsm6dsx_shub_write_reg(hw
, slv_addr
, config
,
736 err
= st_lsm6dsx_shub_master_enable(sensor
, true);
740 st_lsm6dsx_shub_wait_complete(hw
);
742 err
= st_lsm6dsx_shub_read_output(hw
, &data
, sizeof(data
));
744 st_lsm6dsx_shub_master_enable(sensor
, false);
749 if (data
!= settings
->wai
.val
)
752 *i2c_addr
= settings
->i2c_addr
[i
];
757 /* reset SLV0 channel */
758 config
[0] = hub_settings
->pause
;
760 config
[2] = slv_config
;
761 err
= st_lsm6dsx_shub_write_reg(hw
, slv_addr
, config
,
766 return found
? 0 : -ENODEV
;
769 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw
*hw
, const char *name
)
771 enum st_lsm6dsx_sensor_id id
= ST_LSM6DSX_ID_EXT0
;
772 struct st_lsm6dsx_sensor
*sensor
;
773 int err
, i
, num_ext_dev
= 0;
776 for (i
= 0; i
< ARRAY_SIZE(st_lsm6dsx_ext_dev_table
); i
++) {
777 err
= st_lsm6dsx_shub_check_wai(hw
, &i2c_addr
,
778 &st_lsm6dsx_ext_dev_table
[i
]);
784 hw
->iio_devs
[id
] = st_lsm6dsx_shub_alloc_iiodev(hw
, id
,
785 &st_lsm6dsx_ext_dev_table
[i
],
787 if (!hw
->iio_devs
[id
])
790 sensor
= iio_priv(hw
->iio_devs
[id
]);
791 err
= st_lsm6dsx_shub_init_device(sensor
);
795 if (++num_ext_dev
>= hw
->settings
->shub_settings
.num_ext_dev
)