1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Capella Microsystems Inc.
4 * Author: Kevin Tsai <ktsai@capellamicro.com>
7 #include <linux/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/events.h>
17 #include <linux/init.h>
19 /* Registers Address */
20 #define CM32181_REG_ADDR_CMD 0x00
21 #define CM32181_REG_ADDR_ALS 0x04
22 #define CM32181_REG_ADDR_STATUS 0x06
23 #define CM32181_REG_ADDR_ID 0x07
25 /* Number of Configurable Registers */
26 #define CM32181_CONF_REG_NUM 0x01
29 #define CM32181_CMD_ALS_ENABLE 0x00
30 #define CM32181_CMD_ALS_DISABLE 0x01
31 #define CM32181_CMD_ALS_INT_EN 0x02
33 #define CM32181_CMD_ALS_IT_SHIFT 6
34 #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT)
35 #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT)
37 #define CM32181_CMD_ALS_SM_SHIFT 11
38 #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT)
39 #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
41 #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */
42 #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */
43 #define CM32181_CALIBSCALE_DEFAULT 1000
44 #define CM32181_CALIBSCALE_RESOLUTION 1000
45 #define MLUX_PER_LUX 1000
47 static const u8 cm32181_reg
[CM32181_CONF_REG_NUM
] = {
51 static const int als_it_bits
[] = {12, 8, 0, 1, 2, 3};
52 static const int als_it_value
[] = {25000, 50000, 100000, 200000, 400000,
56 struct i2c_client
*client
;
58 u16 conf_regs
[CM32181_CONF_REG_NUM
];
63 * cm32181_reg_init() - Initialize CM32181 registers
64 * @cm32181: pointer of struct cm32181.
66 * Initialize CM32181 ambient light sensor register to default values.
68 * Return: 0 for success; otherwise for error code.
70 static int cm32181_reg_init(struct cm32181_chip
*cm32181
)
72 struct i2c_client
*client
= cm32181
->client
;
76 ret
= i2c_smbus_read_word_data(client
, CM32181_REG_ADDR_ID
);
81 if ((ret
& 0xFF) != 0x81)
85 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
] = CM32181_CMD_ALS_ENABLE
|
86 CM32181_CMD_ALS_IT_DEFAULT
| CM32181_CMD_ALS_SM_DEFAULT
;
87 cm32181
->calibscale
= CM32181_CALIBSCALE_DEFAULT
;
89 /* Initialize registers*/
90 for (i
= 0; i
< CM32181_CONF_REG_NUM
; i
++) {
91 ret
= i2c_smbus_write_word_data(client
, cm32181_reg
[i
],
92 cm32181
->conf_regs
[i
]);
101 * cm32181_read_als_it() - Get sensor integration time (ms)
102 * @cm32181: pointer of struct cm32181
103 * @val2: pointer of int to load the als_it value.
105 * Report the current integartion time by millisecond.
107 * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
109 static int cm32181_read_als_it(struct cm32181_chip
*cm32181
, int *val2
)
114 als_it
= cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
];
115 als_it
&= CM32181_CMD_ALS_IT_MASK
;
116 als_it
>>= CM32181_CMD_ALS_IT_SHIFT
;
117 for (i
= 0; i
< ARRAY_SIZE(als_it_bits
); i
++) {
118 if (als_it
== als_it_bits
[i
]) {
119 *val2
= als_it_value
[i
];
120 return IIO_VAL_INT_PLUS_MICRO
;
128 * cm32181_write_als_it() - Write sensor integration time
129 * @cm32181: pointer of struct cm32181.
130 * @val: integration time by millisecond.
132 * Convert integration time (ms) to sensor value.
134 * Return: i2c_smbus_write_word_data command return value.
136 static int cm32181_write_als_it(struct cm32181_chip
*cm32181
, int val
)
138 struct i2c_client
*client
= cm32181
->client
;
142 n
= ARRAY_SIZE(als_it_value
);
143 for (i
= 0; i
< n
; i
++)
144 if (val
<= als_it_value
[i
])
149 als_it
= als_it_bits
[i
];
150 als_it
<<= CM32181_CMD_ALS_IT_SHIFT
;
152 mutex_lock(&cm32181
->lock
);
153 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
] &=
154 ~CM32181_CMD_ALS_IT_MASK
;
155 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
] |=
157 ret
= i2c_smbus_write_word_data(client
, CM32181_REG_ADDR_CMD
,
158 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
]);
159 mutex_unlock(&cm32181
->lock
);
165 * cm32181_get_lux() - report current lux value
166 * @cm32181: pointer of struct cm32181.
168 * Convert sensor raw data to lux. It depends on integration
169 * time and calibscale variable.
171 * Return: Positive value is lux, otherwise is error code.
173 static int cm32181_get_lux(struct cm32181_chip
*cm32181
)
175 struct i2c_client
*client
= cm32181
->client
;
180 ret
= cm32181_read_als_it(cm32181
, &als_it
);
184 lux
= CM32181_MLUX_PER_BIT
;
185 lux
*= CM32181_MLUX_PER_BIT_BASE_IT
;
188 ret
= i2c_smbus_read_word_data(client
, CM32181_REG_ADDR_ALS
);
193 lux
*= cm32181
->calibscale
;
194 lux
/= CM32181_CALIBSCALE_RESOLUTION
;
203 static int cm32181_read_raw(struct iio_dev
*indio_dev
,
204 struct iio_chan_spec
const *chan
,
205 int *val
, int *val2
, long mask
)
207 struct cm32181_chip
*cm32181
= iio_priv(indio_dev
);
211 case IIO_CHAN_INFO_PROCESSED
:
212 ret
= cm32181_get_lux(cm32181
);
217 case IIO_CHAN_INFO_CALIBSCALE
:
218 *val
= cm32181
->calibscale
;
220 case IIO_CHAN_INFO_INT_TIME
:
222 ret
= cm32181_read_als_it(cm32181
, val2
);
229 static int cm32181_write_raw(struct iio_dev
*indio_dev
,
230 struct iio_chan_spec
const *chan
,
231 int val
, int val2
, long mask
)
233 struct cm32181_chip
*cm32181
= iio_priv(indio_dev
);
237 case IIO_CHAN_INFO_CALIBSCALE
:
238 cm32181
->calibscale
= val
;
240 case IIO_CHAN_INFO_INT_TIME
:
241 ret
= cm32181_write_als_it(cm32181
, val2
);
249 * cm32181_get_it_available() - Get available ALS IT value
250 * @dev: pointer of struct device.
251 * @attr: pointer of struct device_attribute.
252 * @buf: pointer of return string buffer.
254 * Display the available integration time values by millisecond.
256 * Return: string length.
258 static ssize_t
cm32181_get_it_available(struct device
*dev
,
259 struct device_attribute
*attr
, char *buf
)
263 n
= ARRAY_SIZE(als_it_value
);
264 for (i
= 0, len
= 0; i
< n
; i
++)
265 len
+= sprintf(buf
+ len
, "0.%06u ", als_it_value
[i
]);
266 return len
+ sprintf(buf
+ len
, "\n");
269 static const struct iio_chan_spec cm32181_channels
[] = {
272 .info_mask_separate
=
273 BIT(IIO_CHAN_INFO_PROCESSED
) |
274 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
275 BIT(IIO_CHAN_INFO_INT_TIME
),
279 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available
,
280 S_IRUGO
, cm32181_get_it_available
, NULL
, 0);
282 static struct attribute
*cm32181_attributes
[] = {
283 &iio_dev_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
287 static const struct attribute_group cm32181_attribute_group
= {
288 .attrs
= cm32181_attributes
291 static const struct iio_info cm32181_info
= {
292 .read_raw
= &cm32181_read_raw
,
293 .write_raw
= &cm32181_write_raw
,
294 .attrs
= &cm32181_attribute_group
,
297 static int cm32181_probe(struct i2c_client
*client
,
298 const struct i2c_device_id
*id
)
300 struct cm32181_chip
*cm32181
;
301 struct iio_dev
*indio_dev
;
304 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*cm32181
));
306 dev_err(&client
->dev
, "devm_iio_device_alloc failed\n");
310 cm32181
= iio_priv(indio_dev
);
311 i2c_set_clientdata(client
, indio_dev
);
312 cm32181
->client
= client
;
314 mutex_init(&cm32181
->lock
);
315 indio_dev
->dev
.parent
= &client
->dev
;
316 indio_dev
->channels
= cm32181_channels
;
317 indio_dev
->num_channels
= ARRAY_SIZE(cm32181_channels
);
318 indio_dev
->info
= &cm32181_info
;
319 indio_dev
->name
= id
->name
;
320 indio_dev
->modes
= INDIO_DIRECT_MODE
;
322 ret
= cm32181_reg_init(cm32181
);
324 dev_err(&client
->dev
,
325 "%s: register init failed\n",
330 ret
= devm_iio_device_register(&client
->dev
, indio_dev
);
332 dev_err(&client
->dev
,
333 "%s: regist device failed\n",
341 static const struct i2c_device_id cm32181_id
[] = {
346 MODULE_DEVICE_TABLE(i2c
, cm32181_id
);
348 static const struct of_device_id cm32181_of_match
[] = {
349 { .compatible
= "capella,cm32181" },
352 MODULE_DEVICE_TABLE(of
, cm32181_of_match
);
354 static struct i2c_driver cm32181_driver
= {
357 .of_match_table
= of_match_ptr(cm32181_of_match
),
359 .id_table
= cm32181_id
,
360 .probe
= cm32181_probe
,
363 module_i2c_driver(cm32181_driver
);
365 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
366 MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
367 MODULE_LICENSE("GPL");