• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/i2c.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/iio/iio.h>
14 #include <linux/regmap.h>
15 
16 #include <linux/iio/common/st_sensors_i2c.h>
17 
18 #define ST_SENSORS_I2C_MULTIREAD	0x80
19 
20 static const struct regmap_config st_sensors_i2c_regmap_config = {
21 	.reg_bits = 8,
22 	.val_bits = 8,
23 };
24 
25 static const struct regmap_config st_sensors_i2c_regmap_multiread_bit_config = {
26 	.reg_bits = 8,
27 	.val_bits = 8,
28 	.read_flag_mask = ST_SENSORS_I2C_MULTIREAD,
29 };
30 
31 /*
32  * st_sensors_i2c_configure() - configure I2C interface
33  * @indio_dev: IIO device reference.
34  * @client: i2c client reference.
35  *
36  * Return: 0 on success, else a negative error code.
37  */
st_sensors_i2c_configure(struct iio_dev * indio_dev,struct i2c_client * client)38 int st_sensors_i2c_configure(struct iio_dev *indio_dev,
39 			     struct i2c_client *client)
40 {
41 	struct st_sensor_data *sdata = iio_priv(indio_dev);
42 	const struct regmap_config *config;
43 
44 	if (sdata->sensor_settings->multi_read_bit)
45 		config = &st_sensors_i2c_regmap_multiread_bit_config;
46 	else
47 		config = &st_sensors_i2c_regmap_config;
48 
49 	sdata->regmap = devm_regmap_init_i2c(client, config);
50 	if (IS_ERR(sdata->regmap)) {
51 		dev_err(&client->dev, "Failed to register i2c regmap (%ld)\n",
52 			PTR_ERR(sdata->regmap));
53 		return PTR_ERR(sdata->regmap);
54 	}
55 
56 	i2c_set_clientdata(client, indio_dev);
57 
58 	indio_dev->name = client->name;
59 
60 	sdata->irq = client->irq;
61 
62 	return 0;
63 }
64 EXPORT_SYMBOL_NS(st_sensors_i2c_configure, IIO_ST_SENSORS);
65 
66 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
67 MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
68 MODULE_LICENSE("GPL v2");
69