1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * CM3232 Ambient Light Sensor
4 *
5 * Copyright (C) 2014-2015 Capella Microsystems Inc.
6 * Author: Kevin Tsai <ktsai@capellamicro.com>
7 *
8 * IIO driver for CM3232 (7-bit I2C slave address 0x10).
9 */
10
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/init.h>
17
18 /* Registers Address */
19 #define CM3232_REG_ADDR_CMD 0x00
20 #define CM3232_REG_ADDR_ALS 0x50
21 #define CM3232_REG_ADDR_ID 0x53
22
23 #define CM3232_CMD_ALS_DISABLE BIT(0)
24
25 #define CM3232_CMD_ALS_IT_SHIFT 2
26 #define CM3232_CMD_ALS_IT_MASK (BIT(2) | BIT(3) | BIT(4))
27 #define CM3232_CMD_ALS_IT_DEFAULT (0x01 << CM3232_CMD_ALS_IT_SHIFT)
28
29 #define CM3232_CMD_ALS_RESET BIT(6)
30
31 #define CM3232_CMD_DEFAULT CM3232_CMD_ALS_IT_DEFAULT
32
33 #define CM3232_HW_ID 0x32
34 #define CM3232_CALIBSCALE_DEFAULT 100000
35 #define CM3232_CALIBSCALE_RESOLUTION 100000
36 #define CM3232_MLUX_PER_LUX 1000
37
38 #define CM3232_MLUX_PER_BIT_DEFAULT 64
39 #define CM3232_MLUX_PER_BIT_BASE_IT 100000
40
41 static const struct {
42 int val;
43 int val2;
44 u8 it;
45 } cm3232_als_it_scales[] = {
46 {0, 100000, 0}, /* 0.100000 */
47 {0, 200000, 1}, /* 0.200000 */
48 {0, 400000, 2}, /* 0.400000 */
49 {0, 800000, 3}, /* 0.800000 */
50 {1, 600000, 4}, /* 1.600000 */
51 {3, 200000, 5}, /* 3.200000 */
52 };
53
54 struct cm3232_als_info {
55 u8 regs_cmd_default;
56 u8 hw_id;
57 int mlux_per_bit;
58 int mlux_per_bit_base_it;
59 };
60
61 static const struct cm3232_als_info cm3232_als_info_default = {
62 .regs_cmd_default = CM3232_CMD_DEFAULT,
63 .hw_id = CM3232_HW_ID,
64 .mlux_per_bit = CM3232_MLUX_PER_BIT_DEFAULT,
65 .mlux_per_bit_base_it = CM3232_MLUX_PER_BIT_BASE_IT,
66 };
67
68 struct cm3232_chip {
69 struct i2c_client *client;
70 const struct cm3232_als_info *als_info;
71 int calibscale;
72 u8 regs_cmd;
73 u16 regs_als;
74 };
75
76 /**
77 * cm3232_reg_init() - Initialize CM3232
78 * @chip: pointer of struct cm3232_chip.
79 *
80 * Check and initialize CM3232 ambient light sensor.
81 *
82 * Return: 0 for success; otherwise for error code.
83 */
cm3232_reg_init(struct cm3232_chip * chip)84 static int cm3232_reg_init(struct cm3232_chip *chip)
85 {
86 struct i2c_client *client = chip->client;
87 s32 ret;
88
89 chip->als_info = &cm3232_als_info_default;
90
91 /* Disable and reset device */
92 chip->regs_cmd = CM3232_CMD_ALS_DISABLE | CM3232_CMD_ALS_RESET;
93 ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
94 chip->regs_cmd);
95 if (ret < 0) {
96 dev_err(&chip->client->dev, "Error writing reg_cmd\n");
97 return ret;
98 }
99
100 /* Identify device */
101 ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ID);
102 if (ret < 0) {
103 dev_err(&chip->client->dev, "Error reading addr_id\n");
104 return ret;
105 }
106
107 if ((ret & 0xFF) != chip->als_info->hw_id)
108 return -ENODEV;
109
110 /* Register default value */
111 chip->regs_cmd = chip->als_info->regs_cmd_default;
112
113 /* Configure register */
114 ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
115 chip->regs_cmd);
116 if (ret < 0)
117 dev_err(&chip->client->dev, "Error writing reg_cmd\n");
118
119 return ret;
120 }
121
122 /**
123 * cm3232_read_als_it() - Get sensor integration time
124 * @chip: pointer of struct cm3232_chip
125 * @val: pointer of int to load the integration (sec).
126 * @val2: pointer of int to load the integration time (microsecond).
127 *
128 * Report the current integration time.
129 *
130 * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
131 */
cm3232_read_als_it(struct cm3232_chip * chip,int * val,int * val2)132 static int cm3232_read_als_it(struct cm3232_chip *chip, int *val, int *val2)
133 {
134 u16 als_it;
135 int i;
136
137 als_it = chip->regs_cmd;
138 als_it &= CM3232_CMD_ALS_IT_MASK;
139 als_it >>= CM3232_CMD_ALS_IT_SHIFT;
140 for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
141 if (als_it == cm3232_als_it_scales[i].it) {
142 *val = cm3232_als_it_scales[i].val;
143 *val2 = cm3232_als_it_scales[i].val2;
144 return IIO_VAL_INT_PLUS_MICRO;
145 }
146 }
147
148 return -EINVAL;
149 }
150
151 /**
152 * cm3232_write_als_it() - Write sensor integration time
153 * @chip: pointer of struct cm3232_chip.
154 * @val: integration time in second.
155 * @val2: integration time in microsecond.
156 *
157 * Convert integration time to sensor value.
158 *
159 * Return: i2c_smbus_write_byte_data command return value.
160 */
cm3232_write_als_it(struct cm3232_chip * chip,int val,int val2)161 static int cm3232_write_als_it(struct cm3232_chip *chip, int val, int val2)
162 {
163 struct i2c_client *client = chip->client;
164 u16 als_it, cmd;
165 int i;
166 s32 ret;
167
168 for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
169 if (val == cm3232_als_it_scales[i].val &&
170 val2 == cm3232_als_it_scales[i].val2) {
171
172 als_it = cm3232_als_it_scales[i].it;
173 als_it <<= CM3232_CMD_ALS_IT_SHIFT;
174
175 cmd = chip->regs_cmd & ~CM3232_CMD_ALS_IT_MASK;
176 cmd |= als_it;
177 ret = i2c_smbus_write_byte_data(client,
178 CM3232_REG_ADDR_CMD,
179 cmd);
180 if (ret < 0)
181 return ret;
182 chip->regs_cmd = cmd;
183 return 0;
184 }
185 }
186 return -EINVAL;
187 }
188
189 /**
190 * cm3232_get_lux() - report current lux value
191 * @chip: pointer of struct cm3232_chip.
192 *
193 * Convert sensor data to lux. It depends on integration
194 * time and calibscale variable.
195 *
196 * Return: Zero or positive value is lux, otherwise error code.
197 */
cm3232_get_lux(struct cm3232_chip * chip)198 static int cm3232_get_lux(struct cm3232_chip *chip)
199 {
200 struct i2c_client *client = chip->client;
201 const struct cm3232_als_info *als_info = chip->als_info;
202 int ret;
203 int val, val2;
204 int als_it;
205 u64 lux;
206
207 /* Calculate mlux per bit based on als_it */
208 ret = cm3232_read_als_it(chip, &val, &val2);
209 if (ret < 0)
210 return -EINVAL;
211 als_it = val * 1000000 + val2;
212 lux = (__force u64)als_info->mlux_per_bit;
213 lux *= als_info->mlux_per_bit_base_it;
214 lux = div_u64(lux, als_it);
215
216 ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ALS);
217 if (ret < 0) {
218 dev_err(&client->dev, "Error reading reg_addr_als\n");
219 return ret;
220 }
221
222 chip->regs_als = (u16)ret;
223 lux *= chip->regs_als;
224 lux *= chip->calibscale;
225 lux = div_u64(lux, CM3232_CALIBSCALE_RESOLUTION);
226 lux = div_u64(lux, CM3232_MLUX_PER_LUX);
227
228 if (lux > 0xFFFF)
229 lux = 0xFFFF;
230
231 return (int)lux;
232 }
233
cm3232_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)234 static int cm3232_read_raw(struct iio_dev *indio_dev,
235 struct iio_chan_spec const *chan,
236 int *val, int *val2, long mask)
237 {
238 struct cm3232_chip *chip = iio_priv(indio_dev);
239 int ret;
240
241 switch (mask) {
242 case IIO_CHAN_INFO_PROCESSED:
243 ret = cm3232_get_lux(chip);
244 if (ret < 0)
245 return ret;
246 *val = ret;
247 return IIO_VAL_INT;
248 case IIO_CHAN_INFO_CALIBSCALE:
249 *val = chip->calibscale;
250 return IIO_VAL_INT;
251 case IIO_CHAN_INFO_INT_TIME:
252 return cm3232_read_als_it(chip, val, val2);
253 }
254
255 return -EINVAL;
256 }
257
cm3232_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)258 static int cm3232_write_raw(struct iio_dev *indio_dev,
259 struct iio_chan_spec const *chan,
260 int val, int val2, long mask)
261 {
262 struct cm3232_chip *chip = iio_priv(indio_dev);
263
264 switch (mask) {
265 case IIO_CHAN_INFO_CALIBSCALE:
266 chip->calibscale = val;
267 return 0;
268 case IIO_CHAN_INFO_INT_TIME:
269 return cm3232_write_als_it(chip, val, val2);
270 }
271
272 return -EINVAL;
273 }
274
275 /**
276 * cm3232_get_it_available() - Get available ALS IT value
277 * @dev: pointer of struct device.
278 * @attr: pointer of struct device_attribute.
279 * @buf: pointer of return string buffer.
280 *
281 * Display the available integration time in second.
282 *
283 * Return: string length.
284 */
cm3232_get_it_available(struct device * dev,struct device_attribute * attr,char * buf)285 static ssize_t cm3232_get_it_available(struct device *dev,
286 struct device_attribute *attr, char *buf)
287 {
288 int i, len;
289
290 for (i = 0, len = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++)
291 len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ",
292 cm3232_als_it_scales[i].val,
293 cm3232_als_it_scales[i].val2);
294 return len + scnprintf(buf + len, PAGE_SIZE - len, "\n");
295 }
296
297 static const struct iio_chan_spec cm3232_channels[] = {
298 {
299 .type = IIO_LIGHT,
300 .info_mask_separate =
301 BIT(IIO_CHAN_INFO_PROCESSED) |
302 BIT(IIO_CHAN_INFO_CALIBSCALE) |
303 BIT(IIO_CHAN_INFO_INT_TIME),
304 }
305 };
306
307 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
308 S_IRUGO, cm3232_get_it_available, NULL, 0);
309
310 static struct attribute *cm3232_attributes[] = {
311 &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
312 NULL,
313 };
314
315 static const struct attribute_group cm3232_attribute_group = {
316 .attrs = cm3232_attributes
317 };
318
319 static const struct iio_info cm3232_info = {
320 .read_raw = &cm3232_read_raw,
321 .write_raw = &cm3232_write_raw,
322 .attrs = &cm3232_attribute_group,
323 };
324
cm3232_probe(struct i2c_client * client)325 static int cm3232_probe(struct i2c_client *client)
326 {
327 const struct i2c_device_id *id = i2c_client_get_device_id(client);
328 struct cm3232_chip *chip;
329 struct iio_dev *indio_dev;
330 int ret;
331
332 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
333 if (!indio_dev)
334 return -ENOMEM;
335
336 chip = iio_priv(indio_dev);
337 i2c_set_clientdata(client, indio_dev);
338 chip->client = client;
339 chip->calibscale = CM3232_CALIBSCALE_DEFAULT;
340
341 indio_dev->channels = cm3232_channels;
342 indio_dev->num_channels = ARRAY_SIZE(cm3232_channels);
343 indio_dev->info = &cm3232_info;
344 indio_dev->name = id->name;
345 indio_dev->modes = INDIO_DIRECT_MODE;
346
347 ret = cm3232_reg_init(chip);
348 if (ret) {
349 dev_err(&client->dev,
350 "%s: register init failed\n",
351 __func__);
352 return ret;
353 }
354
355 return iio_device_register(indio_dev);
356 }
357
cm3232_remove(struct i2c_client * client)358 static void cm3232_remove(struct i2c_client *client)
359 {
360 struct iio_dev *indio_dev = i2c_get_clientdata(client);
361
362 i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
363 CM3232_CMD_ALS_DISABLE);
364
365 iio_device_unregister(indio_dev);
366 }
367
368 static const struct i2c_device_id cm3232_id[] = {
369 { "cm3232" },
370 { }
371 };
372
cm3232_suspend(struct device * dev)373 static int cm3232_suspend(struct device *dev)
374 {
375 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
376 struct cm3232_chip *chip = iio_priv(indio_dev);
377 struct i2c_client *client = chip->client;
378 int ret;
379
380 chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
381 ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
382 chip->regs_cmd);
383
384 return ret;
385 }
386
cm3232_resume(struct device * dev)387 static int cm3232_resume(struct device *dev)
388 {
389 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
390 struct cm3232_chip *chip = iio_priv(indio_dev);
391 struct i2c_client *client = chip->client;
392 int ret;
393
394 chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
395 ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
396 chip->regs_cmd | CM3232_CMD_ALS_RESET);
397
398 return ret;
399 }
400
401 static DEFINE_SIMPLE_DEV_PM_OPS(cm3232_pm_ops, cm3232_suspend, cm3232_resume);
402
403 MODULE_DEVICE_TABLE(i2c, cm3232_id);
404
405 static const struct of_device_id cm3232_of_match[] = {
406 {.compatible = "capella,cm3232"},
407 { }
408 };
409 MODULE_DEVICE_TABLE(of, cm3232_of_match);
410
411 static struct i2c_driver cm3232_driver = {
412 .driver = {
413 .name = "cm3232",
414 .of_match_table = cm3232_of_match,
415 .pm = pm_sleep_ptr(&cm3232_pm_ops),
416 },
417 .id_table = cm3232_id,
418 .probe = cm3232_probe,
419 .remove = cm3232_remove,
420 };
421
422 module_i2c_driver(cm3232_driver);
423
424 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
425 MODULE_DESCRIPTION("CM3232 ambient light sensor driver");
426 MODULE_LICENSE("GPL");
427