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/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/pm.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/types.h>
13 
14 #include <linux/iio/iio.h>
15 
16 #define AL3000A_REG_SYSTEM		0x00
17 #define AL3000A_REG_DATA		0x05
18 
19 #define AL3000A_CONFIG_ENABLE		0x00
20 #define AL3000A_CONFIG_DISABLE		0x0b
21 #define AL3000A_CONFIG_RESET		0x0f
22 #define AL3000A_GAIN_MASK		GENMASK(5, 0)
23 
24 /*
25  * These are pre-calculated lux values based on possible output of sensor
26  * (range 0x00 - 0x3F)
27  */
28 static const u32 lux_table[] = {
29 	1, 1, 1, 2, 2, 2, 3, 4,					/* 0 - 7 */
30 	4, 5, 6, 7, 9, 11, 13, 16,				/* 8 - 15 */
31 	19, 22, 27, 32, 39, 46, 56, 67,				/* 16 - 23 */
32 	80, 96, 116, 139, 167, 200, 240, 289,			/* 24 - 31 */
33 	347, 416, 499, 600, 720, 864, 1037, 1245,		/* 32 - 39 */
34 	1495, 1795, 2155, 2587, 3105, 3728, 4475, 5373,		/* 40 - 47 */
35 	6450, 7743, 9296, 11160, 13397, 16084, 19309, 23180,	/* 48 - 55 */
36 	27828, 33408, 40107, 48148, 57803, 69393, 83306, 100000 /* 56 - 63 */
37 };
38 
39 static const struct regmap_config al3000a_regmap_config = {
40 	.reg_bits = 8,
41 	.val_bits = 8,
42 	.max_register = AL3000A_REG_DATA,
43 };
44 
45 struct al3000a_data {
46 	struct regmap *regmap;
47 	struct regulator *vdd_supply;
48 };
49 
50 static const struct iio_chan_spec al3000a_channels[] = {
51 	{
52 		.type = IIO_LIGHT,
53 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
54 	},
55 };
56 
al3000a_set_pwr_on(struct al3000a_data * data)57 static int al3000a_set_pwr_on(struct al3000a_data *data)
58 {
59 	struct device *dev = regmap_get_device(data->regmap);
60 	int ret;
61 
62 	ret = regulator_enable(data->vdd_supply);
63 	if (ret) {
64 		dev_err(dev, "failed to enable vdd power supply\n");
65 		return ret;
66 	}
67 
68 	return regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_ENABLE);
69 }
70 
al3000a_set_pwr_off(void * _data)71 static void al3000a_set_pwr_off(void *_data)
72 {
73 	struct al3000a_data *data = _data;
74 	struct device *dev = regmap_get_device(data->regmap);
75 	int ret;
76 
77 	ret = regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_DISABLE);
78 	if (ret)
79 		dev_err(dev, "failed to write system register\n");
80 
81 	ret = regulator_disable(data->vdd_supply);
82 	if (ret)
83 		dev_err(dev, "failed to disable vdd power supply\n");
84 }
85 
al3000a_init(struct al3000a_data * data)86 static int al3000a_init(struct al3000a_data *data)
87 {
88 	int ret;
89 
90 	ret = al3000a_set_pwr_on(data);
91 	if (ret)
92 		return ret;
93 
94 	ret = regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_RESET);
95 	if (ret)
96 		return ret;
97 
98 	return regmap_write(data->regmap, AL3000A_REG_SYSTEM, AL3000A_CONFIG_ENABLE);
99 }
100 
al3000a_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)101 static int al3000a_read_raw(struct iio_dev *indio_dev,
102 			    struct iio_chan_spec const *chan, int *val,
103 			    int *val2, long mask)
104 {
105 	struct al3000a_data *data = iio_priv(indio_dev);
106 	int ret, gain;
107 
108 	switch (mask) {
109 	case IIO_CHAN_INFO_PROCESSED:
110 		ret = regmap_read(data->regmap, AL3000A_REG_DATA, &gain);
111 		if (ret)
112 			return ret;
113 
114 		*val = lux_table[gain & AL3000A_GAIN_MASK];
115 
116 		return IIO_VAL_INT;
117 	default:
118 		return -EINVAL;
119 	}
120 }
121 
122 static const struct iio_info al3000a_info = {
123 	.read_raw = al3000a_read_raw,
124 };
125 
al3000a_probe(struct i2c_client * client)126 static int al3000a_probe(struct i2c_client *client)
127 {
128 	struct al3000a_data *data;
129 	struct device *dev = &client->dev;
130 	struct iio_dev *indio_dev;
131 	int ret;
132 
133 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
134 	if (!indio_dev)
135 		return -ENOMEM;
136 
137 	data = iio_priv(indio_dev);
138 	i2c_set_clientdata(client, indio_dev);
139 
140 	data->regmap = devm_regmap_init_i2c(client, &al3000a_regmap_config);
141 	if (IS_ERR(data->regmap))
142 		return dev_err_probe(dev, PTR_ERR(data->regmap),
143 				     "cannot allocate regmap\n");
144 
145 	data->vdd_supply = devm_regulator_get(dev, "vdd");
146 	if (IS_ERR(data->vdd_supply))
147 		return dev_err_probe(dev, PTR_ERR(data->vdd_supply),
148 				     "failed to get vdd regulator\n");
149 
150 	indio_dev->info = &al3000a_info;
151 	indio_dev->name = "al3000a";
152 	indio_dev->channels = al3000a_channels;
153 	indio_dev->num_channels = ARRAY_SIZE(al3000a_channels);
154 	indio_dev->modes = INDIO_DIRECT_MODE;
155 
156 	ret = al3000a_init(data);
157 	if (ret)
158 		return dev_err_probe(dev, ret, "failed to init ALS\n");
159 
160 	ret = devm_add_action_or_reset(dev, al3000a_set_pwr_off, data);
161 	if (ret)
162 		return dev_err_probe(dev, ret, "failed to add action\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 	{ "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 	{ /* sentinel */ }
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