xref: /linux/drivers/iio/light/cm32181.c (revision f50f98310e51b3712a4e544c75615e89c8233125)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Capella Microsystems Inc.
4  * Author: Kevin Tsai <ktsai@capellamicro.com>
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/i2c.h>
10 #include <linux/mutex.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/interrupt.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/events.h>
18 #include <linux/init.h>
19 
20 /* Registers Address */
21 #define CM32181_REG_ADDR_CMD		0x00
22 #define CM32181_REG_ADDR_WH		0x01
23 #define CM32181_REG_ADDR_WL		0x02
24 #define CM32181_REG_ADDR_TEST		0x03
25 #define CM32181_REG_ADDR_ALS		0x04
26 #define CM32181_REG_ADDR_STATUS		0x06
27 #define CM32181_REG_ADDR_ID		0x07
28 
29 /* Number of Configurable Registers */
30 #define CM32181_CONF_REG_NUM		4
31 
32 /* CMD register */
33 #define CM32181_CMD_ALS_DISABLE		BIT(0)
34 #define CM32181_CMD_ALS_INT_EN		BIT(1)
35 #define CM32181_CMD_ALS_THRES_WINDOW	BIT(2)
36 
37 #define CM32181_CMD_ALS_PERS_SHIFT	4
38 #define CM32181_CMD_ALS_PERS_MASK	(0x03 << CM32181_CMD_ALS_PERS_SHIFT)
39 #define CM32181_CMD_ALS_PERS_DEFAULT	(0x01 << CM32181_CMD_ALS_PERS_SHIFT)
40 
41 #define CM32181_CMD_ALS_IT_SHIFT	6
42 #define CM32181_CMD_ALS_IT_MASK		(0x0F << CM32181_CMD_ALS_IT_SHIFT)
43 #define CM32181_CMD_ALS_IT_DEFAULT	(0x00 << CM32181_CMD_ALS_IT_SHIFT)
44 
45 #define CM32181_CMD_ALS_SM_SHIFT	11
46 #define CM32181_CMD_ALS_SM_MASK		(0x03 << CM32181_CMD_ALS_SM_SHIFT)
47 #define CM32181_CMD_ALS_SM_DEFAULT	(0x01 << CM32181_CMD_ALS_SM_SHIFT)
48 
49 #define CM32181_LUX_PER_BIT		500	/* ALS_SM=01 IT=800ms */
50 #define CM32181_LUX_PER_BIT_RESOLUTION	100000
51 #define CM32181_LUX_PER_BIT_BASE_IT	800000	/* Based on IT=800ms */
52 #define CM32181_CALIBSCALE_DEFAULT	100000
53 #define CM32181_CALIBSCALE_RESOLUTION	100000
54 
55 #define SMBUS_ALERT_RESPONSE_ADDRESS	0x0c
56 
57 /* CM3218 Family */
58 static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 };
59 static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 };
60 
61 /* CM32181 Family */
62 static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 };
63 static const int cm32181_als_it_values[] = {
64 	25000, 50000, 100000, 200000, 400000, 800000
65 };
66 
67 struct cm32181_chip {
68 	struct i2c_client *client;
69 	struct mutex lock;
70 	u16 conf_regs[CM32181_CONF_REG_NUM];
71 	unsigned long init_regs_bitmap;
72 	int calibscale;
73 	int num_als_it;
74 	const int *als_it_bits;
75 	const int *als_it_values;
76 };
77 
78 /**
79  * cm32181_reg_init() - Initialize CM32181 registers
80  * @cm32181:	pointer of struct cm32181.
81  *
82  * Initialize CM32181 ambient light sensor register to default values.
83  *
84  * Return: 0 for success; otherwise for error code.
85  */
86 static int cm32181_reg_init(struct cm32181_chip *cm32181)
87 {
88 	struct i2c_client *client = cm32181->client;
89 	int i;
90 	s32 ret;
91 
92 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
93 	if (ret < 0)
94 		return ret;
95 
96 	/* check device ID */
97 	switch (ret & 0xFF) {
98 	case 0x18: /* CM3218 */
99 		cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits);
100 		cm32181->als_it_bits = cm3218_als_it_bits;
101 		cm32181->als_it_values = cm3218_als_it_values;
102 		break;
103 	case 0x81: /* CM32181 */
104 	case 0x82: /* CM32182, fully compat. with CM32181 */
105 		cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits);
106 		cm32181->als_it_bits = cm32181_als_it_bits;
107 		cm32181->als_it_values = cm32181_als_it_values;
108 		break;
109 	default:
110 		return -ENODEV;
111 	}
112 
113 	/* Default Values */
114 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] =
115 			CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
116 	cm32181->init_regs_bitmap = BIT(CM32181_REG_ADDR_CMD);
117 	cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
118 
119 	/* Initialize registers*/
120 	for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) {
121 		ret = i2c_smbus_write_word_data(client, i,
122 						cm32181->conf_regs[i]);
123 		if (ret < 0)
124 			return ret;
125 	}
126 
127 	return 0;
128 }
129 
130 /**
131  *  cm32181_read_als_it() - Get sensor integration time (ms)
132  *  @cm32181:	pointer of struct cm32181
133  *  @val2:	pointer of int to load the als_it value.
134  *
135  *  Report the current integartion time by millisecond.
136  *
137  *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
138  */
139 static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
140 {
141 	u16 als_it;
142 	int i;
143 
144 	als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
145 	als_it &= CM32181_CMD_ALS_IT_MASK;
146 	als_it >>= CM32181_CMD_ALS_IT_SHIFT;
147 	for (i = 0; i < cm32181->num_als_it; i++) {
148 		if (als_it == cm32181->als_it_bits[i]) {
149 			*val2 = cm32181->als_it_values[i];
150 			return IIO_VAL_INT_PLUS_MICRO;
151 		}
152 	}
153 
154 	return -EINVAL;
155 }
156 
157 /**
158  * cm32181_write_als_it() - Write sensor integration time
159  * @cm32181:	pointer of struct cm32181.
160  * @val:	integration time by millisecond.
161  *
162  * Convert integration time (ms) to sensor value.
163  *
164  * Return: i2c_smbus_write_word_data command return value.
165  */
166 static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
167 {
168 	struct i2c_client *client = cm32181->client;
169 	u16 als_it;
170 	int ret, i, n;
171 
172 	n = cm32181->num_als_it;
173 	for (i = 0; i < n; i++)
174 		if (val <= cm32181->als_it_values[i])
175 			break;
176 	if (i >= n)
177 		i = n - 1;
178 
179 	als_it = cm32181->als_it_bits[i];
180 	als_it <<= CM32181_CMD_ALS_IT_SHIFT;
181 
182 	mutex_lock(&cm32181->lock);
183 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
184 		~CM32181_CMD_ALS_IT_MASK;
185 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
186 		als_it;
187 	ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
188 			cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
189 	mutex_unlock(&cm32181->lock);
190 
191 	return ret;
192 }
193 
194 /**
195  * cm32181_get_lux() - report current lux value
196  * @cm32181:	pointer of struct cm32181.
197  *
198  * Convert sensor raw data to lux.  It depends on integration
199  * time and calibscale variable.
200  *
201  * Return: Positive value is lux, otherwise is error code.
202  */
203 static int cm32181_get_lux(struct cm32181_chip *cm32181)
204 {
205 	struct i2c_client *client = cm32181->client;
206 	int ret;
207 	int als_it;
208 	u64 lux;
209 
210 	ret = cm32181_read_als_it(cm32181, &als_it);
211 	if (ret < 0)
212 		return -EINVAL;
213 
214 	lux = CM32181_LUX_PER_BIT;
215 	lux *= CM32181_LUX_PER_BIT_BASE_IT;
216 	lux = div_u64(lux, als_it);
217 
218 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
219 	if (ret < 0)
220 		return ret;
221 
222 	lux *= ret;
223 	lux *= cm32181->calibscale;
224 	lux = div_u64(lux, CM32181_CALIBSCALE_RESOLUTION);
225 	lux = div_u64(lux, CM32181_LUX_PER_BIT_RESOLUTION);
226 
227 	if (lux > 0xFFFF)
228 		lux = 0xFFFF;
229 
230 	return lux;
231 }
232 
233 static int cm32181_read_raw(struct iio_dev *indio_dev,
234 			    struct iio_chan_spec const *chan,
235 			    int *val, int *val2, long mask)
236 {
237 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
238 	int ret;
239 
240 	switch (mask) {
241 	case IIO_CHAN_INFO_PROCESSED:
242 		ret = cm32181_get_lux(cm32181);
243 		if (ret < 0)
244 			return ret;
245 		*val = ret;
246 		return IIO_VAL_INT;
247 	case IIO_CHAN_INFO_CALIBSCALE:
248 		*val = cm32181->calibscale;
249 		return IIO_VAL_INT;
250 	case IIO_CHAN_INFO_INT_TIME:
251 		*val = 0;
252 		ret = cm32181_read_als_it(cm32181, val2);
253 		return ret;
254 	}
255 
256 	return -EINVAL;
257 }
258 
259 static int cm32181_write_raw(struct iio_dev *indio_dev,
260 			     struct iio_chan_spec const *chan,
261 			     int val, int val2, long mask)
262 {
263 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
264 	int ret;
265 
266 	switch (mask) {
267 	case IIO_CHAN_INFO_CALIBSCALE:
268 		cm32181->calibscale = val;
269 		return val;
270 	case IIO_CHAN_INFO_INT_TIME:
271 		ret = cm32181_write_als_it(cm32181, val2);
272 		return ret;
273 	}
274 
275 	return -EINVAL;
276 }
277 
278 /**
279  * cm32181_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.
283  *
284  * Display the available integration time values by millisecond.
285  *
286  * Return: string length.
287  */
288 static ssize_t cm32181_get_it_available(struct device *dev,
289 			struct device_attribute *attr, char *buf)
290 {
291 	struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev));
292 	int i, n, len;
293 
294 	n = cm32181->num_als_it;
295 	for (i = 0, len = 0; i < n; i++)
296 		len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]);
297 	return len + sprintf(buf + len, "\n");
298 }
299 
300 static const struct iio_chan_spec cm32181_channels[] = {
301 	{
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),
307 	}
308 };
309 
310 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
311 			S_IRUGO, cm32181_get_it_available, NULL, 0);
312 
313 static struct attribute *cm32181_attributes[] = {
314 	&iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
315 	NULL,
316 };
317 
318 static const struct attribute_group cm32181_attribute_group = {
319 	.attrs = cm32181_attributes
320 };
321 
322 static const struct iio_info cm32181_info = {
323 	.read_raw		= &cm32181_read_raw,
324 	.write_raw		= &cm32181_write_raw,
325 	.attrs			= &cm32181_attribute_group,
326 };
327 
328 static int cm32181_probe(struct i2c_client *client)
329 {
330 	struct device *dev = &client->dev;
331 	struct cm32181_chip *cm32181;
332 	struct iio_dev *indio_dev;
333 	int ret;
334 
335 	indio_dev = devm_iio_device_alloc(dev, sizeof(*cm32181));
336 	if (!indio_dev)
337 		return -ENOMEM;
338 
339 	/*
340 	 * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the
341 	 * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address.
342 	 * Detect this and take the following step to deal with it:
343 	 * 1. When a SMBus Alert capable sensor has an Alert asserted, it will
344 	 *    not respond on its actual I2C address. Read a byte from the ARA
345 	 *    to clear any pending Alerts.
346 	 * 2. Create a "dummy" client for the actual I2C address and
347 	 *    use that client to communicate with the sensor.
348 	 */
349 	if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {
350 		struct i2c_board_info board_info = { .type = "dummy" };
351 
352 		i2c_smbus_read_byte(client);
353 
354 		client = i2c_acpi_new_device(dev, 1, &board_info);
355 		if (IS_ERR(client))
356 			return PTR_ERR(client);
357 	}
358 
359 	cm32181 = iio_priv(indio_dev);
360 	cm32181->client = client;
361 
362 	mutex_init(&cm32181->lock);
363 	indio_dev->dev.parent = dev;
364 	indio_dev->channels = cm32181_channels;
365 	indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
366 	indio_dev->info = &cm32181_info;
367 	indio_dev->name = dev_name(dev);
368 	indio_dev->modes = INDIO_DIRECT_MODE;
369 
370 	ret = cm32181_reg_init(cm32181);
371 	if (ret) {
372 		dev_err(dev, "%s: register init failed\n", __func__);
373 		return ret;
374 	}
375 
376 	ret = devm_iio_device_register(dev, indio_dev);
377 	if (ret) {
378 		dev_err(dev, "%s: regist device failed\n", __func__);
379 		return ret;
380 	}
381 
382 	return 0;
383 }
384 
385 static const struct of_device_id cm32181_of_match[] = {
386 	{ .compatible = "capella,cm3218" },
387 	{ .compatible = "capella,cm32181" },
388 	{ }
389 };
390 MODULE_DEVICE_TABLE(of, cm32181_of_match);
391 
392 #ifdef CONFIG_ACPI
393 static const struct acpi_device_id cm32181_acpi_match[] = {
394 	{ "CPLM3218", 0 },
395 	{ }
396 };
397 MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match);
398 #endif
399 
400 static struct i2c_driver cm32181_driver = {
401 	.driver = {
402 		.name	= "cm32181",
403 		.acpi_match_table = ACPI_PTR(cm32181_acpi_match),
404 		.of_match_table = cm32181_of_match,
405 	},
406 	.probe_new	= cm32181_probe,
407 };
408 
409 module_i2c_driver(cm32181_driver);
410 
411 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
412 MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
413 MODULE_LICENSE("GPL");
414