xref: /linux/drivers/iio/light/al3000a.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/array_size.h>
3 #include <linux/bitfield.h>
4 #include <linux/device.h>
5 #include <linux/err.h>
6 #include <linux/i2c.h>
7 #include <linux/module.h>
8 #include <linux/pm.h>
9 #include <linux/regmap.h>
10 #include <linux/regulator/consumer.h>
11 #include <linux/types.h>
12 
13 #include <linux/iio/iio.h>
14 
15 #define AL3000A_REG_SYSTEM		0x00
16 #define AL3000A_REG_DATA		0x05
17 
18 #define AL3000A_CONFIG_ENABLE		0x00
19 #define AL3000A_CONFIG_DISABLE		0x0b
20 #define AL3000A_CONFIG_RESET		0x0f
21 #define AL3000A_GAIN_MASK		GENMASK(5, 0)
22 
23 /*
24  * These are pre-calculated lux values based on possible output of sensor
25  * (range 0x00 - 0x3F)
26  */
27 static const u32 lux_table[] = {
28 	1, 1, 1, 2, 2, 2, 3, 4,					/* 0 - 7 */
29 	4, 5, 6, 7, 9, 11, 13, 16,				/* 8 - 15 */
30 	19, 22, 27, 32, 39, 46, 56, 67,				/* 16 - 23 */
31 	80, 96, 116, 139, 167, 200, 240, 289,			/* 24 - 31 */
32 	347, 416, 499, 600, 720, 864, 1037, 1245,		/* 32 - 39 */
33 	1495, 1795, 2155, 2587, 3105, 3728, 4475, 5373,		/* 40 - 47 */
34 	6450, 7743, 9296, 11160, 13397, 16084, 19309, 23180,	/* 48 - 55 */
35 	27828, 33408, 40107, 48148, 57803, 69393, 83306, 100000 /* 56 - 63 */
36 };
37 
38 static const struct regmap_config al3000a_regmap_config = {
39 	.reg_bits = 8,
40 	.val_bits = 8,
41 	.max_register = AL3000A_REG_DATA,
42 };
43 
44 struct al3000a_data {
45 	struct regmap *regmap;
46 	struct regulator *vdd_supply;
47 };
48 
49 static const struct iio_chan_spec al3000a_channels[] = {
50 	{
51 		.type = IIO_LIGHT,
52 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
53 	},
54 };
55 
al3000a_set_pwr_on(struct al3000a_data * data)56 static int al3000a_set_pwr_on(struct al3000a_data *data)
57 {
58 	struct device *dev = regmap_get_device(data->regmap);
59 	int ret;
60 
61 	ret = regulator_enable(data->vdd_supply);
62 	if (ret) {
63 		dev_err(dev, "failed to enable vdd power supply\n");
64 		return ret;
65 	}
66 
67 	return regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_ENABLE);
68 }
69 
al3000a_set_pwr_off(void * _data)70 static void al3000a_set_pwr_off(void *_data)
71 {
72 	struct al3000a_data *data = _data;
73 	struct device *dev = regmap_get_device(data->regmap);
74 	int ret;
75 
76 	ret = regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_DISABLE);
77 	if (ret)
78 		dev_err(dev, "failed to write system register\n");
79 
80 	ret = regulator_disable(data->vdd_supply);
81 	if (ret)
82 		dev_err(dev, "failed to disable vdd power supply\n");
83 }
84 
al3000a_init(struct al3000a_data * data)85 static int al3000a_init(struct al3000a_data *data)
86 {
87 	struct device *dev = regmap_get_device(data->regmap);
88 	int ret;
89 
90 	ret = al3000a_set_pwr_on(data);
91 	if (ret)
92 		return ret;
93 
94 	ret = devm_add_action_or_reset(dev, al3000a_set_pwr_off, data);
95 	if (ret)
96 		return ret;
97 
98 	ret = regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_RESET);
99 	if (ret)
100 		return ret;
101 
102 	return regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_ENABLE);
103 }
104 
al3000a_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)105 static int al3000a_read_raw(struct iio_dev *indio_dev,
106 			    struct iio_chan_spec const *chan, int *val,
107 			    int *val2, long mask)
108 {
109 	struct al3000a_data *data = iio_priv(indio_dev);
110 	int ret, gain;
111 
112 	switch (mask) {
113 	case IIO_CHAN_INFO_PROCESSED:
114 		ret = regmap_read(data->regmap, AL3000A_REG_DATA, &gain);
115 		if (ret)
116 			return ret;
117 
118 		*val = lux_table[gain & AL3000A_GAIN_MASK];
119 
120 		return IIO_VAL_INT;
121 	default:
122 		return -EINVAL;
123 	}
124 }
125 
126 static const struct iio_info al3000a_info = {
127 	.read_raw = al3000a_read_raw,
128 };
129 
al3000a_probe(struct i2c_client * client)130 static int al3000a_probe(struct i2c_client *client)
131 {
132 	struct al3000a_data *data;
133 	struct device *dev = &client->dev;
134 	struct iio_dev *indio_dev;
135 	int ret;
136 
137 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
138 	if (!indio_dev)
139 		return -ENOMEM;
140 
141 	data = iio_priv(indio_dev);
142 	i2c_set_clientdata(client, indio_dev);
143 
144 	data->regmap = devm_regmap_init_i2c(client, &al3000a_regmap_config);
145 	if (IS_ERR(data->regmap))
146 		return dev_err_probe(dev, PTR_ERR(data->regmap),
147 				     "cannot allocate regmap\n");
148 
149 	data->vdd_supply = devm_regulator_get(dev, "vdd");
150 	if (IS_ERR(data->vdd_supply))
151 		return dev_err_probe(dev, PTR_ERR(data->vdd_supply),
152 				     "failed to get vdd regulator\n");
153 
154 	indio_dev->info = &al3000a_info;
155 	indio_dev->name = "al3000a";
156 	indio_dev->channels = al3000a_channels;
157 	indio_dev->num_channels = ARRAY_SIZE(al3000a_channels);
158 	indio_dev->modes = INDIO_DIRECT_MODE;
159 
160 	ret = al3000a_init(data);
161 	if (ret)
162 		return dev_err_probe(dev, ret, "failed to init ALS\n");
163 
164 	return devm_iio_device_register(dev, indio_dev);
165 }
166 
al3000a_suspend(struct device * dev)167 static int al3000a_suspend(struct device *dev)
168 {
169 	struct al3000a_data *data = iio_priv(dev_get_drvdata(dev));
170 
171 	al3000a_set_pwr_off(data);
172 	return 0;
173 }
174 
al3000a_resume(struct device * dev)175 static int al3000a_resume(struct device *dev)
176 {
177 	struct al3000a_data *data = iio_priv(dev_get_drvdata(dev));
178 
179 	return al3000a_set_pwr_on(data);
180 }
181 
182 static DEFINE_SIMPLE_DEV_PM_OPS(al3000a_pm_ops, al3000a_suspend, al3000a_resume);
183 
184 static const struct i2c_device_id al3000a_id[] = {
185 	{ .name = "al3000a" },
186 	{ }
187 };
188 MODULE_DEVICE_TABLE(i2c, al3000a_id);
189 
190 static const struct of_device_id al3000a_of_match[] = {
191 	{ .compatible = "dynaimage,al3000a" },
192 	{ }
193 };
194 MODULE_DEVICE_TABLE(of, al3000a_of_match);
195 
196 static struct i2c_driver al3000a_driver = {
197 	.driver = {
198 		.name = "al3000a",
199 		.of_match_table = al3000a_of_match,
200 		.pm = pm_sleep_ptr(&al3000a_pm_ops),
201 	},
202 	.probe = al3000a_probe,
203 	.id_table = al3000a_id,
204 };
205 module_i2c_driver(al3000a_driver);
206 
207 MODULE_AUTHOR("Svyatolsav Ryhel <clamor95@gmail.com>");
208 MODULE_DESCRIPTION("al3000a Ambient Light Sensor driver");
209 MODULE_LICENSE("GPL");
210