Merge tag 'sound-5.8-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[platform/kernel/linux-starfive.git] / drivers / iio / common / st_sensors / st_sensors_i2c.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics sensors i2c 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/iio/iio.h>
14 #include <linux/regmap.h>
15
16 #include <linux/iio/common/st_sensors_i2c.h>
17
18
19 #define ST_SENSORS_I2C_MULTIREAD        0x80
20
21 static const struct regmap_config st_sensors_i2c_regmap_config = {
22         .reg_bits = 8,
23         .val_bits = 8,
24 };
25
26 static const struct regmap_config st_sensors_i2c_regmap_multiread_bit_config = {
27         .reg_bits = 8,
28         .val_bits = 8,
29         .read_flag_mask = ST_SENSORS_I2C_MULTIREAD,
30 };
31
32 /*
33  * st_sensors_i2c_configure() - configure I2C interface
34  * @indio_dev: IIO device reference.
35  * @client: i2c client reference.
36  *
37  * Return: 0 on success, else a negative error code.
38  */
39 int st_sensors_i2c_configure(struct iio_dev *indio_dev,
40                              struct i2c_client *client)
41 {
42         struct st_sensor_data *sdata = iio_priv(indio_dev);
43         const struct regmap_config *config;
44
45         if (sdata->sensor_settings->multi_read_bit)
46                 config = &st_sensors_i2c_regmap_multiread_bit_config;
47         else
48                 config = &st_sensors_i2c_regmap_config;
49
50         sdata->regmap = devm_regmap_init_i2c(client, config);
51         if (IS_ERR(sdata->regmap)) {
52                 dev_err(&client->dev, "Failed to register i2c regmap (%ld)\n",
53                         PTR_ERR(sdata->regmap));
54                 return PTR_ERR(sdata->regmap);
55         }
56
57         i2c_set_clientdata(client, indio_dev);
58
59         indio_dev->dev.parent = &client->dev;
60         indio_dev->name = client->name;
61
62         sdata->dev = &client->dev;
63         sdata->irq = client->irq;
64
65         return 0;
66 }
67 EXPORT_SYMBOL(st_sensors_i2c_configure);
68
69 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
70 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
71 MODULE_LICENSE("GPL v2");