WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / light / cm3232.c
blob18a410340dc563813583718a3165992df889e91f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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).
9 */
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
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 */
54 struct cm3232_als_info {
55 u8 regs_cmd_default;
56 u8 hw_id;
57 int calibscale;
58 int mlux_per_bit;
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,
70 struct cm3232_chip {
71 struct i2c_client *client;
72 struct cm3232_als_info *als_info;
73 u8 regs_cmd;
74 u16 regs_als;
77 /**
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;
88 s32 ret;
90 chip->als_info = &cm3232_als_info_default;
92 /* Identify device */
93 ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ID);
94 if (ret < 0) {
95 dev_err(&chip->client->dev, "Error reading addr_id\n");
96 return ret;
99 if ((ret & 0xFF) != chip->als_info->hw_id)
100 return -ENODEV;
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,
105 chip->regs_cmd);
106 if (ret < 0) {
107 dev_err(&chip->client->dev, "Error writing reg_cmd\n");
108 return ret;
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,
116 chip->regs_cmd);
117 if (ret < 0)
118 dev_err(&chip->client->dev, "Error writing reg_cmd\n");
120 return ret;
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)
135 u16 als_it;
136 int i;
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;
149 return -EINVAL;
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;
165 u16 als_it, cmd;
166 int i;
167 s32 ret;
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;
177 cmd |= als_it;
178 ret = i2c_smbus_write_byte_data(client,
179 CM3232_REG_ADDR_CMD,
180 cmd);
181 if (ret < 0)
182 return ret;
183 chip->regs_cmd = cmd;
184 return 0;
187 return -EINVAL;
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;
203 int ret;
204 int val, val2;
205 int als_it;
206 u64 lux;
208 /* Calculate mlux per bit based on als_it */
209 ret = cm3232_read_als_it(chip, &val, &val2);
210 if (ret < 0)
211 return -EINVAL;
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);
218 if (ret < 0) {
219 dev_err(&client->dev, "Error reading reg_addr_als\n");
220 return ret;
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);
229 if (lux > 0xFFFF)
230 lux = 0xFFFF;
232 return (int)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;
241 int ret;
243 switch (mask) {
244 case IIO_CHAN_INFO_PROCESSED:
245 ret = cm3232_get_lux(chip);
246 if (ret < 0)
247 return ret;
248 *val = ret;
249 return IIO_VAL_INT;
250 case IIO_CHAN_INFO_CALIBSCALE:
251 *val = als_info->calibscale;
252 return IIO_VAL_INT;
253 case IIO_CHAN_INFO_INT_TIME:
254 return cm3232_read_als_it(chip, val, val2);
257 return -EINVAL;
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;
267 switch (mask) {
268 case IIO_CHAN_INFO_CALIBSCALE:
269 als_info->calibscale = val;
270 return 0;
271 case IIO_CHAN_INFO_INT_TIME:
272 return cm3232_write_als_it(chip, val, val2);
275 return -EINVAL;
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)
291 int i, len;
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[] = {
302 .type = IIO_LIGHT,
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,
315 NULL,
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;
333 int ret;
335 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
336 if (!indio_dev)
337 return -ENOMEM;
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);
350 if (ret) {
351 dev_err(&client->dev,
352 "%s: register init failed\n",
353 __func__);
354 return ret;
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);
369 return 0;
372 static const struct i2c_device_id cm3232_id[] = {
373 {"cm3232", 0},
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;
383 int ret;
385 chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
386 ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
387 chip->regs_cmd);
389 return ret;
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;
397 int ret;
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);
403 return ret;
406 static const struct dev_pm_ops cm3232_pm_ops = {
407 SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
408 #endif
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 = {
419 .driver = {
420 .name = "cm3232",
421 .of_match_table = cm3232_of_match,
422 #ifdef CONFIG_PM_SLEEP
423 .pm = &cm3232_pm_ops,
424 #endif
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");