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/mod_devicetable.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/init.h>
18 /* Registers Address */
19 #define CM3232_REG_ADDR_CMD 0x00
20 #define CM3232_REG_ADDR_ALS 0x50
21 #define CM3232_REG_ADDR_ID 0x53
23 #define CM3232_CMD_ALS_DISABLE BIT(0)
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)
29 #define CM3232_CMD_ALS_RESET BIT(6)
31 #define CM3232_CMD_DEFAULT CM3232_CMD_ALS_IT_DEFAULT
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
38 #define CM3232_MLUX_PER_BIT_DEFAULT 64
39 #define CM3232_MLUX_PER_BIT_BASE_IT 100000
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 */
54 struct cm3232_als_info
{
59 int mlux_per_bit_base_it
;
62 static struct cm3232_als_info cm3232_als_info_default
= {
63 .regs_cmd_default
= CM3232_CMD_DEFAULT
,
64 .hw_id
= CM3232_HW_ID
,
65 .calibscale
= CM3232_CALIBSCALE_DEFAULT
,
66 .mlux_per_bit
= CM3232_MLUX_PER_BIT_DEFAULT
,
67 .mlux_per_bit_base_it
= CM3232_MLUX_PER_BIT_BASE_IT
,
71 struct i2c_client
*client
;
72 struct cm3232_als_info
*als_info
;
78 * cm3232_reg_init() - Initialize CM3232
79 * @chip: pointer of struct cm3232_chip.
81 * Check and initialize CM3232 ambient light sensor.
83 * Return: 0 for success; otherwise for error code.
85 static int cm3232_reg_init(struct cm3232_chip
*chip
)
87 struct i2c_client
*client
= chip
->client
;
90 chip
->als_info
= &cm3232_als_info_default
;
93 ret
= i2c_smbus_read_word_data(client
, CM3232_REG_ADDR_ID
);
95 dev_err(&chip
->client
->dev
, "Error reading addr_id\n");
99 if ((ret
& 0xFF) != chip
->als_info
->hw_id
)
102 /* Disable and reset device */
103 chip
->regs_cmd
= CM3232_CMD_ALS_DISABLE
| CM3232_CMD_ALS_RESET
;
104 ret
= i2c_smbus_write_byte_data(client
, CM3232_REG_ADDR_CMD
,
107 dev_err(&chip
->client
->dev
, "Error writing reg_cmd\n");
111 /* Register default value */
112 chip
->regs_cmd
= chip
->als_info
->regs_cmd_default
;
114 /* Configure register */
115 ret
= i2c_smbus_write_byte_data(client
, CM3232_REG_ADDR_CMD
,
118 dev_err(&chip
->client
->dev
, "Error writing reg_cmd\n");
124 * cm3232_read_als_it() - Get sensor integration time
125 * @chip: pointer of struct cm3232_chip
126 * @val: pointer of int to load the integration (sec).
127 * @val2: pointer of int to load the integration time (microsecond).
129 * Report the current integration time.
131 * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
133 static int cm3232_read_als_it(struct cm3232_chip
*chip
, int *val
, int *val2
)
138 als_it
= chip
->regs_cmd
;
139 als_it
&= CM3232_CMD_ALS_IT_MASK
;
140 als_it
>>= CM3232_CMD_ALS_IT_SHIFT
;
141 for (i
= 0; i
< ARRAY_SIZE(cm3232_als_it_scales
); i
++) {
142 if (als_it
== cm3232_als_it_scales
[i
].it
) {
143 *val
= cm3232_als_it_scales
[i
].val
;
144 *val2
= cm3232_als_it_scales
[i
].val2
;
145 return IIO_VAL_INT_PLUS_MICRO
;
153 * cm3232_write_als_it() - Write sensor integration time
154 * @chip: pointer of struct cm3232_chip.
155 * @val: integration time in second.
156 * @val2: integration time in microsecond.
158 * Convert integration time to sensor value.
160 * Return: i2c_smbus_write_byte_data command return value.
162 static int cm3232_write_als_it(struct cm3232_chip
*chip
, int val
, int val2
)
164 struct i2c_client
*client
= chip
->client
;
169 for (i
= 0; i
< ARRAY_SIZE(cm3232_als_it_scales
); i
++) {
170 if (val
== cm3232_als_it_scales
[i
].val
&&
171 val2
== cm3232_als_it_scales
[i
].val2
) {
173 als_it
= cm3232_als_it_scales
[i
].it
;
174 als_it
<<= CM3232_CMD_ALS_IT_SHIFT
;
176 cmd
= chip
->regs_cmd
& ~CM3232_CMD_ALS_IT_MASK
;
178 ret
= i2c_smbus_write_byte_data(client
,
183 chip
->regs_cmd
= cmd
;
191 * cm3232_get_lux() - report current lux value
192 * @chip: pointer of struct cm3232_chip.
194 * Convert sensor data to lux. It depends on integration
195 * time and calibscale variable.
197 * Return: Zero or positive value is lux, otherwise error code.
199 static int cm3232_get_lux(struct cm3232_chip
*chip
)
201 struct i2c_client
*client
= chip
->client
;
202 struct cm3232_als_info
*als_info
= chip
->als_info
;
208 /* Calculate mlux per bit based on als_it */
209 ret
= cm3232_read_als_it(chip
, &val
, &val2
);
212 als_it
= val
* 1000000 + val2
;
213 lux
= (__force u64
)als_info
->mlux_per_bit
;
214 lux
*= als_info
->mlux_per_bit_base_it
;
215 lux
= div_u64(lux
, als_it
);
217 ret
= i2c_smbus_read_word_data(client
, CM3232_REG_ADDR_ALS
);
219 dev_err(&client
->dev
, "Error reading reg_addr_als\n");
223 chip
->regs_als
= (u16
)ret
;
224 lux
*= chip
->regs_als
;
225 lux
*= als_info
->calibscale
;
226 lux
= div_u64(lux
, CM3232_CALIBSCALE_RESOLUTION
);
227 lux
= div_u64(lux
, CM3232_MLUX_PER_LUX
);
235 static int cm3232_read_raw(struct iio_dev
*indio_dev
,
236 struct iio_chan_spec
const *chan
,
237 int *val
, int *val2
, long mask
)
239 struct cm3232_chip
*chip
= iio_priv(indio_dev
);
240 struct cm3232_als_info
*als_info
= chip
->als_info
;
244 case IIO_CHAN_INFO_PROCESSED
:
245 ret
= cm3232_get_lux(chip
);
250 case IIO_CHAN_INFO_CALIBSCALE
:
251 *val
= als_info
->calibscale
;
253 case IIO_CHAN_INFO_INT_TIME
:
254 return cm3232_read_als_it(chip
, val
, val2
);
260 static int cm3232_write_raw(struct iio_dev
*indio_dev
,
261 struct iio_chan_spec
const *chan
,
262 int val
, int val2
, long mask
)
264 struct cm3232_chip
*chip
= iio_priv(indio_dev
);
265 struct cm3232_als_info
*als_info
= chip
->als_info
;
268 case IIO_CHAN_INFO_CALIBSCALE
:
269 als_info
->calibscale
= val
;
271 case IIO_CHAN_INFO_INT_TIME
:
272 return cm3232_write_als_it(chip
, val
, val2
);
279 * cm3232_get_it_available() - Get available ALS IT value
280 * @dev: pointer of struct device.
281 * @attr: pointer of struct device_attribute.
282 * @buf: pointer of return string buffer.
284 * Display the available integration time in second.
286 * Return: string length.
288 static ssize_t
cm3232_get_it_available(struct device
*dev
,
289 struct device_attribute
*attr
, char *buf
)
293 for (i
= 0, len
= 0; i
< ARRAY_SIZE(cm3232_als_it_scales
); i
++)
294 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%u.%06u ",
295 cm3232_als_it_scales
[i
].val
,
296 cm3232_als_it_scales
[i
].val2
);
297 return len
+ scnprintf(buf
+ len
, PAGE_SIZE
- len
, "\n");
300 static const struct iio_chan_spec cm3232_channels
[] = {
303 .info_mask_separate
=
304 BIT(IIO_CHAN_INFO_PROCESSED
) |
305 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
306 BIT(IIO_CHAN_INFO_INT_TIME
),
310 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available
,
311 S_IRUGO
, cm3232_get_it_available
, NULL
, 0);
313 static struct attribute
*cm3232_attributes
[] = {
314 &iio_dev_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
318 static const struct attribute_group cm3232_attribute_group
= {
319 .attrs
= cm3232_attributes
322 static const struct iio_info cm3232_info
= {
323 .read_raw
= &cm3232_read_raw
,
324 .write_raw
= &cm3232_write_raw
,
325 .attrs
= &cm3232_attribute_group
,
328 static int cm3232_probe(struct i2c_client
*client
,
329 const struct i2c_device_id
*id
)
331 struct cm3232_chip
*chip
;
332 struct iio_dev
*indio_dev
;
335 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*chip
));
339 chip
= iio_priv(indio_dev
);
340 i2c_set_clientdata(client
, indio_dev
);
341 chip
->client
= client
;
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
= 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");