1 // SPDX-License-Identifier: GPL-2.0-only
3 * CM3232 Ambient Light Sensor
5 * Copyright (C) 2014-2015 Capella Microsystems Inc.
6 * Author: Kevin Tsai <ktsai@capellamicro.com>
8 * IIO driver for CM3232 (7-bit I2C slave address 0x10).
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/init.h>
17 /* Registers Address */
18 #define CM3232_REG_ADDR_CMD 0x00
19 #define CM3232_REG_ADDR_ALS 0x50
20 #define CM3232_REG_ADDR_ID 0x53
22 #define CM3232_CMD_ALS_DISABLE BIT(0)
24 #define CM3232_CMD_ALS_IT_SHIFT 2
25 #define CM3232_CMD_ALS_IT_MASK (BIT(2) | BIT(3) | BIT(4))
26 #define CM3232_CMD_ALS_IT_DEFAULT (0x01 << CM3232_CMD_ALS_IT_SHIFT)
28 #define CM3232_CMD_ALS_RESET BIT(6)
30 #define CM3232_CMD_DEFAULT CM3232_CMD_ALS_IT_DEFAULT
32 #define CM3232_HW_ID 0x32
33 #define CM3232_CALIBSCALE_DEFAULT 100000
34 #define CM3232_CALIBSCALE_RESOLUTION 100000
35 #define CM3232_MLUX_PER_LUX 1000
37 #define CM3232_MLUX_PER_BIT_DEFAULT 64
38 #define CM3232_MLUX_PER_BIT_BASE_IT 100000
44 } cm3232_als_it_scales
[] = {
45 {0, 100000, 0}, /* 0.100000 */
46 {0, 200000, 1}, /* 0.200000 */
47 {0, 400000, 2}, /* 0.400000 */
48 {0, 800000, 3}, /* 0.800000 */
49 {1, 600000, 4}, /* 1.600000 */
50 {3, 200000, 5}, /* 3.200000 */
53 struct cm3232_als_info
{
58 int mlux_per_bit_base_it
;
61 static struct cm3232_als_info cm3232_als_info_default
= {
62 .regs_cmd_default
= CM3232_CMD_DEFAULT
,
63 .hw_id
= CM3232_HW_ID
,
64 .calibscale
= CM3232_CALIBSCALE_DEFAULT
,
65 .mlux_per_bit
= CM3232_MLUX_PER_BIT_DEFAULT
,
66 .mlux_per_bit_base_it
= CM3232_MLUX_PER_BIT_BASE_IT
,
70 struct i2c_client
*client
;
71 struct cm3232_als_info
*als_info
;
77 * cm3232_reg_init() - Initialize CM3232
78 * @chip: pointer of struct cm3232_chip.
80 * Check and initialize CM3232 ambient light sensor.
82 * Return: 0 for success; otherwise for error code.
84 static int cm3232_reg_init(struct cm3232_chip
*chip
)
86 struct i2c_client
*client
= chip
->client
;
89 chip
->als_info
= &cm3232_als_info_default
;
92 ret
= i2c_smbus_read_word_data(client
, CM3232_REG_ADDR_ID
);
94 dev_err(&chip
->client
->dev
, "Error reading addr_id\n");
98 if ((ret
& 0xFF) != chip
->als_info
->hw_id
)
101 /* Disable and reset device */
102 chip
->regs_cmd
= CM3232_CMD_ALS_DISABLE
| CM3232_CMD_ALS_RESET
;
103 ret
= i2c_smbus_write_byte_data(client
, CM3232_REG_ADDR_CMD
,
106 dev_err(&chip
->client
->dev
, "Error writing reg_cmd\n");
110 /* Register default value */
111 chip
->regs_cmd
= chip
->als_info
->regs_cmd_default
;
113 /* Configure register */
114 ret
= i2c_smbus_write_byte_data(client
, CM3232_REG_ADDR_CMD
,
117 dev_err(&chip
->client
->dev
, "Error writing reg_cmd\n");
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).
128 * Report the current integration time.
130 * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
132 static int cm3232_read_als_it(struct cm3232_chip
*chip
, int *val
, int *val2
)
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
;
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.
157 * Convert integration time to sensor value.
159 * Return: i2c_smbus_write_byte_data command return value.
161 static int cm3232_write_als_it(struct cm3232_chip
*chip
, int val
, int val2
)
163 struct i2c_client
*client
= chip
->client
;
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
) {
172 als_it
= cm3232_als_it_scales
[i
].it
;
173 als_it
<<= CM3232_CMD_ALS_IT_SHIFT
;
175 cmd
= chip
->regs_cmd
& ~CM3232_CMD_ALS_IT_MASK
;
177 ret
= i2c_smbus_write_byte_data(client
,
182 chip
->regs_cmd
= cmd
;
190 * cm3232_get_lux() - report current lux value
191 * @chip: pointer of struct cm3232_chip.
193 * Convert sensor data to lux. It depends on integration
194 * time and calibscale variable.
196 * Return: Zero or positive value is lux, otherwise error code.
198 static int cm3232_get_lux(struct cm3232_chip
*chip
)
200 struct i2c_client
*client
= chip
->client
;
201 struct cm3232_als_info
*als_info
= chip
->als_info
;
207 /* Calculate mlux per bit based on als_it */
208 ret
= cm3232_read_als_it(chip
, &val
, &val2
);
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
);
216 ret
= i2c_smbus_read_word_data(client
, CM3232_REG_ADDR_ALS
);
218 dev_err(&client
->dev
, "Error reading reg_addr_als\n");
222 chip
->regs_als
= (u16
)ret
;
223 lux
*= chip
->regs_als
;
224 lux
*= als_info
->calibscale
;
225 lux
= div_u64(lux
, CM3232_CALIBSCALE_RESOLUTION
);
226 lux
= div_u64(lux
, CM3232_MLUX_PER_LUX
);
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
)
238 struct cm3232_chip
*chip
= iio_priv(indio_dev
);
239 struct cm3232_als_info
*als_info
= chip
->als_info
;
243 case IIO_CHAN_INFO_PROCESSED
:
244 ret
= cm3232_get_lux(chip
);
249 case IIO_CHAN_INFO_CALIBSCALE
:
250 *val
= als_info
->calibscale
;
252 case IIO_CHAN_INFO_INT_TIME
:
253 return cm3232_read_als_it(chip
, val
, val2
);
259 static int cm3232_write_raw(struct iio_dev
*indio_dev
,
260 struct iio_chan_spec
const *chan
,
261 int val
, int val2
, long mask
)
263 struct cm3232_chip
*chip
= iio_priv(indio_dev
);
264 struct cm3232_als_info
*als_info
= chip
->als_info
;
267 case IIO_CHAN_INFO_CALIBSCALE
:
268 als_info
->calibscale
= val
;
270 case IIO_CHAN_INFO_INT_TIME
:
271 return cm3232_write_als_it(chip
, val
, val2
);
278 * cm3232_get_it_available() - Get available ALS IT value
279 * @dev: pointer of struct device.
280 * @attr: pointer of struct device_attribute.
281 * @buf: pointer of return string buffer.
283 * Display the available integration time in second.
285 * Return: string length.
287 static ssize_t
cm3232_get_it_available(struct device
*dev
,
288 struct device_attribute
*attr
, char *buf
)
292 for (i
= 0, len
= 0; i
< ARRAY_SIZE(cm3232_als_it_scales
); i
++)
293 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%u.%06u ",
294 cm3232_als_it_scales
[i
].val
,
295 cm3232_als_it_scales
[i
].val2
);
296 return len
+ scnprintf(buf
+ len
, PAGE_SIZE
- len
, "\n");
299 static const struct iio_chan_spec cm3232_channels
[] = {
302 .info_mask_separate
=
303 BIT(IIO_CHAN_INFO_PROCESSED
) |
304 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
305 BIT(IIO_CHAN_INFO_INT_TIME
),
309 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available
,
310 S_IRUGO
, cm3232_get_it_available
, NULL
, 0);
312 static struct attribute
*cm3232_attributes
[] = {
313 &iio_dev_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
317 static const struct attribute_group cm3232_attribute_group
= {
318 .attrs
= cm3232_attributes
321 static const struct iio_info cm3232_info
= {
322 .read_raw
= &cm3232_read_raw
,
323 .write_raw
= &cm3232_write_raw
,
324 .attrs
= &cm3232_attribute_group
,
327 static int cm3232_probe(struct i2c_client
*client
,
328 const struct i2c_device_id
*id
)
330 struct cm3232_chip
*chip
;
331 struct iio_dev
*indio_dev
;
334 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*chip
));
338 chip
= iio_priv(indio_dev
);
339 i2c_set_clientdata(client
, indio_dev
);
340 chip
->client
= client
;
342 indio_dev
->dev
.parent
= &client
->dev
;
343 indio_dev
->channels
= cm3232_channels
;
344 indio_dev
->num_channels
= ARRAY_SIZE(cm3232_channels
);
345 indio_dev
->info
= &cm3232_info
;
346 indio_dev
->name
= id
->name
;
347 indio_dev
->modes
= INDIO_DIRECT_MODE
;
349 ret
= cm3232_reg_init(chip
);
351 dev_err(&client
->dev
,
352 "%s: register init failed\n",
357 return iio_device_register(indio_dev
);
360 static int cm3232_remove(struct i2c_client
*client
)
362 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
364 i2c_smbus_write_byte_data(client
, CM3232_REG_ADDR_CMD
,
365 CM3232_CMD_ALS_DISABLE
);
367 iio_device_unregister(indio_dev
);
372 static const struct i2c_device_id cm3232_id
[] = {
377 #ifdef CONFIG_PM_SLEEP
378 static int cm3232_suspend(struct device
*dev
)
380 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
381 struct cm3232_chip
*chip
= iio_priv(indio_dev
);
382 struct i2c_client
*client
= chip
->client
;
385 chip
->regs_cmd
|= CM3232_CMD_ALS_DISABLE
;
386 ret
= i2c_smbus_write_byte_data(client
, CM3232_REG_ADDR_CMD
,
392 static int cm3232_resume(struct device
*dev
)
394 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
395 struct cm3232_chip
*chip
= iio_priv(indio_dev
);
396 struct i2c_client
*client
= chip
->client
;
399 chip
->regs_cmd
&= ~CM3232_CMD_ALS_DISABLE
;
400 ret
= i2c_smbus_write_byte_data(client
, CM3232_REG_ADDR_CMD
,
401 chip
->regs_cmd
| CM3232_CMD_ALS_RESET
);
406 static const struct dev_pm_ops cm3232_pm_ops
= {
407 SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend
, cm3232_resume
)};
410 MODULE_DEVICE_TABLE(i2c
, cm3232_id
);
412 static const struct of_device_id cm3232_of_match
[] = {
413 {.compatible
= "capella,cm3232"},
416 MODULE_DEVICE_TABLE(of
, cm3232_of_match
);
418 static struct i2c_driver cm3232_driver
= {
421 .of_match_table
= of_match_ptr(cm3232_of_match
),
422 #ifdef CONFIG_PM_SLEEP
423 .pm
= &cm3232_pm_ops
,
426 .id_table
= cm3232_id
,
427 .probe
= cm3232_probe
,
428 .remove
= cm3232_remove
,
431 module_i2c_driver(cm3232_driver
);
433 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
434 MODULE_DESCRIPTION("CM3232 ambient light sensor driver");
435 MODULE_LICENSE("GPL");