2 * Copyright (C) 2013 Capella Microsystems Inc.
3 * Author: Kevin Tsai <ktsai@capellamicro.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2, as published
7 * by the Free Software Foundation.
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/mutex.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/events.h>
20 #include <linux/init.h>
22 /* Registers Address */
23 #define CM32181_REG_ADDR_CMD 0x00
24 #define CM32181_REG_ADDR_ALS 0x04
25 #define CM32181_REG_ADDR_STATUS 0x06
26 #define CM32181_REG_ADDR_ID 0x07
28 /* Number of Configurable Registers */
29 #define CM32181_CONF_REG_NUM 0x01
32 #define CM32181_CMD_ALS_ENABLE 0x00
33 #define CM32181_CMD_ALS_DISABLE 0x01
34 #define CM32181_CMD_ALS_INT_EN 0x02
36 #define CM32181_CMD_ALS_IT_SHIFT 6
37 #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT)
38 #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT)
40 #define CM32181_CMD_ALS_SM_SHIFT 11
41 #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT)
42 #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
44 #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */
45 #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */
46 #define CM32181_CALIBSCALE_DEFAULT 1000
47 #define CM32181_CALIBSCALE_RESOLUTION 1000
48 #define MLUX_PER_LUX 1000
50 static const u8 cm32181_reg
[CM32181_CONF_REG_NUM
] = {
54 static const int als_it_bits
[] = {12, 8, 0, 1, 2, 3};
55 static const int als_it_value
[] = {25000, 50000, 100000, 200000, 400000,
59 struct i2c_client
*client
;
61 u16 conf_regs
[CM32181_CONF_REG_NUM
];
66 * cm32181_reg_init() - Initialize CM32181 registers
67 * @cm32181: pointer of struct cm32181.
69 * Initialize CM32181 ambient light sensor register to default values.
71 * Return: 0 for success; otherwise for error code.
73 static int cm32181_reg_init(struct cm32181_chip
*cm32181
)
75 struct i2c_client
*client
= cm32181
->client
;
79 ret
= i2c_smbus_read_word_data(client
, CM32181_REG_ADDR_ID
);
84 if ((ret
& 0xFF) != 0x81)
88 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
] = CM32181_CMD_ALS_ENABLE
|
89 CM32181_CMD_ALS_IT_DEFAULT
| CM32181_CMD_ALS_SM_DEFAULT
;
90 cm32181
->calibscale
= CM32181_CALIBSCALE_DEFAULT
;
92 /* Initialize registers*/
93 for (i
= 0; i
< CM32181_CONF_REG_NUM
; i
++) {
94 ret
= i2c_smbus_write_word_data(client
, cm32181_reg
[i
],
95 cm32181
->conf_regs
[i
]);
104 * cm32181_read_als_it() - Get sensor integration time (ms)
105 * @cm32181: pointer of struct cm32181
106 * @val2: pointer of int to load the als_it value.
108 * Report the current integartion time by millisecond.
110 * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
112 static int cm32181_read_als_it(struct cm32181_chip
*cm32181
, int *val2
)
117 als_it
= cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
];
118 als_it
&= CM32181_CMD_ALS_IT_MASK
;
119 als_it
>>= CM32181_CMD_ALS_IT_SHIFT
;
120 for (i
= 0; i
< ARRAY_SIZE(als_it_bits
); i
++) {
121 if (als_it
== als_it_bits
[i
]) {
122 *val2
= als_it_value
[i
];
123 return IIO_VAL_INT_PLUS_MICRO
;
131 * cm32181_write_als_it() - Write sensor integration time
132 * @cm32181: pointer of struct cm32181.
133 * @val: integration time by millisecond.
135 * Convert integration time (ms) to sensor value.
137 * Return: i2c_smbus_write_word_data command return value.
139 static int cm32181_write_als_it(struct cm32181_chip
*cm32181
, int val
)
141 struct i2c_client
*client
= cm32181
->client
;
145 n
= ARRAY_SIZE(als_it_value
);
146 for (i
= 0; i
< n
; i
++)
147 if (val
<= als_it_value
[i
])
152 als_it
= als_it_bits
[i
];
153 als_it
<<= CM32181_CMD_ALS_IT_SHIFT
;
155 mutex_lock(&cm32181
->lock
);
156 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
] &=
157 ~CM32181_CMD_ALS_IT_MASK
;
158 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
] |=
160 ret
= i2c_smbus_write_word_data(client
, CM32181_REG_ADDR_CMD
,
161 cm32181
->conf_regs
[CM32181_REG_ADDR_CMD
]);
162 mutex_unlock(&cm32181
->lock
);
168 * cm32181_get_lux() - report current lux value
169 * @cm32181: pointer of struct cm32181.
171 * Convert sensor raw data to lux. It depends on integration
172 * time and calibscale variable.
174 * Return: Positive value is lux, otherwise is error code.
176 static int cm32181_get_lux(struct cm32181_chip
*cm32181
)
178 struct i2c_client
*client
= cm32181
->client
;
183 ret
= cm32181_read_als_it(cm32181
, &als_it
);
187 lux
= CM32181_MLUX_PER_BIT
;
188 lux
*= CM32181_MLUX_PER_BIT_BASE_IT
;
191 ret
= i2c_smbus_read_word_data(client
, CM32181_REG_ADDR_ALS
);
196 lux
*= cm32181
->calibscale
;
197 lux
/= CM32181_CALIBSCALE_RESOLUTION
;
206 static int cm32181_read_raw(struct iio_dev
*indio_dev
,
207 struct iio_chan_spec
const *chan
,
208 int *val
, int *val2
, long mask
)
210 struct cm32181_chip
*cm32181
= iio_priv(indio_dev
);
214 case IIO_CHAN_INFO_PROCESSED
:
215 ret
= cm32181_get_lux(cm32181
);
220 case IIO_CHAN_INFO_CALIBSCALE
:
221 *val
= cm32181
->calibscale
;
223 case IIO_CHAN_INFO_INT_TIME
:
225 ret
= cm32181_read_als_it(cm32181
, val2
);
232 static int cm32181_write_raw(struct iio_dev
*indio_dev
,
233 struct iio_chan_spec
const *chan
,
234 int val
, int val2
, long mask
)
236 struct cm32181_chip
*cm32181
= iio_priv(indio_dev
);
240 case IIO_CHAN_INFO_CALIBSCALE
:
241 cm32181
->calibscale
= val
;
243 case IIO_CHAN_INFO_INT_TIME
:
244 ret
= cm32181_write_als_it(cm32181
, val2
);
252 * cm32181_get_it_available() - Get available ALS IT value
253 * @dev: pointer of struct device.
254 * @attr: pointer of struct device_attribute.
255 * @buf: pointer of return string buffer.
257 * Display the available integration time values by millisecond.
259 * Return: string length.
261 static ssize_t
cm32181_get_it_available(struct device
*dev
,
262 struct device_attribute
*attr
, char *buf
)
266 n
= ARRAY_SIZE(als_it_value
);
267 for (i
= 0, len
= 0; i
< n
; i
++)
268 len
+= sprintf(buf
+ len
, "0.%06u ", als_it_value
[i
]);
269 return len
+ sprintf(buf
+ len
, "\n");
272 static const struct iio_chan_spec cm32181_channels
[] = {
275 .info_mask_separate
=
276 BIT(IIO_CHAN_INFO_PROCESSED
) |
277 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
278 BIT(IIO_CHAN_INFO_INT_TIME
),
282 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available
,
283 S_IRUGO
, cm32181_get_it_available
, NULL
, 0);
285 static struct attribute
*cm32181_attributes
[] = {
286 &iio_dev_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
290 static const struct attribute_group cm32181_attribute_group
= {
291 .attrs
= cm32181_attributes
294 static const struct iio_info cm32181_info
= {
295 .driver_module
= THIS_MODULE
,
296 .read_raw
= &cm32181_read_raw
,
297 .write_raw
= &cm32181_write_raw
,
298 .attrs
= &cm32181_attribute_group
,
301 static int cm32181_probe(struct i2c_client
*client
,
302 const struct i2c_device_id
*id
)
304 struct cm32181_chip
*cm32181
;
305 struct iio_dev
*indio_dev
;
308 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*cm32181
));
310 dev_err(&client
->dev
, "devm_iio_device_alloc failed\n");
314 cm32181
= iio_priv(indio_dev
);
315 i2c_set_clientdata(client
, indio_dev
);
316 cm32181
->client
= client
;
318 mutex_init(&cm32181
->lock
);
319 indio_dev
->dev
.parent
= &client
->dev
;
320 indio_dev
->channels
= cm32181_channels
;
321 indio_dev
->num_channels
= ARRAY_SIZE(cm32181_channels
);
322 indio_dev
->info
= &cm32181_info
;
323 indio_dev
->name
= id
->name
;
324 indio_dev
->modes
= INDIO_DIRECT_MODE
;
326 ret
= cm32181_reg_init(cm32181
);
328 dev_err(&client
->dev
,
329 "%s: register init failed\n",
334 ret
= devm_iio_device_register(&client
->dev
, indio_dev
);
336 dev_err(&client
->dev
,
337 "%s: regist device failed\n",
345 static const struct i2c_device_id cm32181_id
[] = {
350 MODULE_DEVICE_TABLE(i2c
, cm32181_id
);
352 static const struct of_device_id cm32181_of_match
[] = {
353 { .compatible
= "capella,cm32181" },
357 static struct i2c_driver cm32181_driver
= {
360 .of_match_table
= of_match_ptr(cm32181_of_match
),
361 .owner
= THIS_MODULE
,
363 .id_table
= cm32181_id
,
364 .probe
= cm32181_probe
,
367 module_i2c_driver(cm32181_driver
);
369 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
370 MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
371 MODULE_LICENSE("GPL");