xref: /linux/drivers/iio/light/cm32181.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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/acpi.h>
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/i2c.h>
11 #include <linux/mutex.h>
12 #include <linux/module.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 /* CPM0 Index 0: device-id (3218 or 32181), 1: Unknown, 2: init_regs_bitmap */
58 #define CPM0_REGS_BITMAP		2
59 #define CPM0_HEADER_SIZE		3
60 
61 /* CPM1 Index 0: lux_per_bit, 1: calibscale, 2: resolution (100000) */
62 #define CPM1_LUX_PER_BIT		0
63 #define CPM1_CALIBSCALE			1
64 #define CPM1_SIZE			3
65 
66 /* CM3218 Family */
67 static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 };
68 static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 };
69 
70 /* CM32181 Family */
71 static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 };
72 static const int cm32181_als_it_values[] = {
73 	25000, 50000, 100000, 200000, 400000, 800000
74 };
75 
76 struct cm32181_chip {
77 	struct i2c_client *client;
78 	struct device *dev;
79 	struct mutex lock;
80 	u16 conf_regs[CM32181_CONF_REG_NUM];
81 	unsigned long init_regs_bitmap;
82 	int calibscale;
83 	int lux_per_bit;
84 	int lux_per_bit_base_it;
85 	int num_als_it;
86 	const int *als_it_bits;
87 	const int *als_it_values;
88 };
89 
90 static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2);
91 
92 #ifdef CONFIG_ACPI
93 /**
94  * cm32181_acpi_get_cpm() - Get CPM object from ACPI
95  * @dev:	pointer of struct device.
96  * @obj_name:	pointer of ACPI object name.
97  * @values:	pointer of array for return elements.
98  * @count:	maximum size of return array.
99  *
100  * Convert ACPI CPM table to array.
101  *
102  * Return: -ENODEV for fail.  Otherwise is number of elements.
103  */
104 static int cm32181_acpi_get_cpm(struct device *dev, char *obj_name,
105 				u64 *values, int count)
106 {
107 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
108 	union acpi_object *cpm, *elem;
109 	acpi_handle handle;
110 	acpi_status status;
111 	int i;
112 
113 	handle = ACPI_HANDLE(dev);
114 	if (!handle)
115 		return -ENODEV;
116 
117 	status = acpi_evaluate_object(handle, obj_name, NULL, &buffer);
118 	if (ACPI_FAILURE(status)) {
119 		dev_err(dev, "object %s not found\n", obj_name);
120 		return -ENODEV;
121 	}
122 
123 	cpm = buffer.pointer;
124 	if (cpm->package.count > count)
125 		dev_warn(dev, "%s table contains %u values, only using first %d values\n",
126 			 obj_name, cpm->package.count, count);
127 
128 	count = min_t(int, cpm->package.count, count);
129 	for (i = 0; i < count; i++) {
130 		elem = &(cpm->package.elements[i]);
131 		values[i] = elem->integer.value;
132 	}
133 
134 	kfree(buffer.pointer);
135 
136 	return count;
137 }
138 
139 static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181)
140 {
141 	u64 vals[CPM0_HEADER_SIZE + CM32181_CONF_REG_NUM];
142 	struct device *dev = cm32181->dev;
143 	int i, count;
144 
145 	count = cm32181_acpi_get_cpm(dev, "CPM0", vals, ARRAY_SIZE(vals));
146 	if (count <= CPM0_HEADER_SIZE)
147 		return;
148 
149 	count -= CPM0_HEADER_SIZE;
150 
151 	cm32181->init_regs_bitmap = vals[CPM0_REGS_BITMAP];
152 	cm32181->init_regs_bitmap &= GENMASK(count - 1, 0);
153 	for_each_set_bit(i, &cm32181->init_regs_bitmap, count)
154 		cm32181->conf_regs[i] =	vals[CPM0_HEADER_SIZE + i];
155 
156 	count = cm32181_acpi_get_cpm(dev, "CPM1", vals, ARRAY_SIZE(vals));
157 	if (count != CPM1_SIZE)
158 		return;
159 
160 	cm32181->lux_per_bit = vals[CPM1_LUX_PER_BIT];
161 
162 	/* Check for uncalibrated devices */
163 	if (vals[CPM1_CALIBSCALE] == CM32181_CALIBSCALE_DEFAULT)
164 		return;
165 
166 	cm32181->calibscale = vals[CPM1_CALIBSCALE];
167 	/* CPM1 lux_per_bit is for the current it value */
168 	cm32181_read_als_it(cm32181, &cm32181->lux_per_bit_base_it);
169 }
170 #else
171 static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181)
172 {
173 }
174 #endif /* CONFIG_ACPI */
175 
176 /**
177  * cm32181_reg_init() - Initialize CM32181 registers
178  * @cm32181:	pointer of struct cm32181.
179  *
180  * Initialize CM32181 ambient light sensor register to default values.
181  *
182  * Return: 0 for success; otherwise for error code.
183  */
184 static int cm32181_reg_init(struct cm32181_chip *cm32181)
185 {
186 	struct i2c_client *client = cm32181->client;
187 	int i;
188 	s32 ret;
189 
190 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
191 	if (ret < 0)
192 		return ret;
193 
194 	/* check device ID */
195 	switch (ret & 0xFF) {
196 	case 0x18: /* CM3218 */
197 		cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits);
198 		cm32181->als_it_bits = cm3218_als_it_bits;
199 		cm32181->als_it_values = cm3218_als_it_values;
200 		break;
201 	case 0x81: /* CM32181 */
202 	case 0x82: /* CM32182, fully compat. with CM32181 */
203 		cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits);
204 		cm32181->als_it_bits = cm32181_als_it_bits;
205 		cm32181->als_it_values = cm32181_als_it_values;
206 		break;
207 	default:
208 		return -ENODEV;
209 	}
210 
211 	/* Default Values */
212 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] =
213 			CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
214 	cm32181->init_regs_bitmap = BIT(CM32181_REG_ADDR_CMD);
215 	cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
216 	cm32181->lux_per_bit = CM32181_LUX_PER_BIT;
217 	cm32181->lux_per_bit_base_it = CM32181_LUX_PER_BIT_BASE_IT;
218 
219 	cm32181_acpi_parse_cpm_tables(cm32181);
220 
221 	/* Initialize registers*/
222 	for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) {
223 		ret = i2c_smbus_write_word_data(client, i,
224 						cm32181->conf_regs[i]);
225 		if (ret < 0)
226 			return ret;
227 	}
228 
229 	return 0;
230 }
231 
232 /**
233  *  cm32181_read_als_it() - Get sensor integration time (ms)
234  *  @cm32181:	pointer of struct cm32181
235  *  @val2:	pointer of int to load the als_it value.
236  *
237  *  Report the current integration time in milliseconds.
238  *
239  *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
240  */
241 static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
242 {
243 	u16 als_it;
244 	int i;
245 
246 	als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
247 	als_it &= CM32181_CMD_ALS_IT_MASK;
248 	als_it >>= CM32181_CMD_ALS_IT_SHIFT;
249 	for (i = 0; i < cm32181->num_als_it; i++) {
250 		if (als_it == cm32181->als_it_bits[i]) {
251 			*val2 = cm32181->als_it_values[i];
252 			return IIO_VAL_INT_PLUS_MICRO;
253 		}
254 	}
255 
256 	return -EINVAL;
257 }
258 
259 /**
260  * cm32181_write_als_it() - Write sensor integration time
261  * @cm32181:	pointer of struct cm32181.
262  * @val:	integration time by millisecond.
263  *
264  * Convert integration time (ms) to sensor value.
265  *
266  * Return: i2c_smbus_write_word_data command return value.
267  */
268 static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
269 {
270 	struct i2c_client *client = cm32181->client;
271 	u16 als_it;
272 	int ret, i, n;
273 
274 	n = cm32181->num_als_it;
275 	for (i = 0; i < n; i++)
276 		if (val <= cm32181->als_it_values[i])
277 			break;
278 	if (i >= n)
279 		i = n - 1;
280 
281 	als_it = cm32181->als_it_bits[i];
282 	als_it <<= CM32181_CMD_ALS_IT_SHIFT;
283 
284 	mutex_lock(&cm32181->lock);
285 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
286 		~CM32181_CMD_ALS_IT_MASK;
287 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
288 		als_it;
289 	ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
290 			cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
291 	mutex_unlock(&cm32181->lock);
292 
293 	return ret;
294 }
295 
296 /**
297  * cm32181_get_lux() - report current lux value
298  * @cm32181:	pointer of struct cm32181.
299  *
300  * Convert sensor raw data to lux.  It depends on integration
301  * time and calibscale variable.
302  *
303  * Return: Positive value is lux, otherwise is error code.
304  */
305 static int cm32181_get_lux(struct cm32181_chip *cm32181)
306 {
307 	struct i2c_client *client = cm32181->client;
308 	int ret;
309 	int als_it;
310 	u64 lux;
311 
312 	ret = cm32181_read_als_it(cm32181, &als_it);
313 	if (ret < 0)
314 		return -EINVAL;
315 
316 	lux = cm32181->lux_per_bit;
317 	lux *= cm32181->lux_per_bit_base_it;
318 	lux = div_u64(lux, als_it);
319 
320 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
321 	if (ret < 0)
322 		return ret;
323 
324 	lux *= ret;
325 	lux *= cm32181->calibscale;
326 	lux = div_u64(lux, CM32181_CALIBSCALE_RESOLUTION);
327 	lux = div_u64(lux, CM32181_LUX_PER_BIT_RESOLUTION);
328 
329 	if (lux > 0xFFFF)
330 		lux = 0xFFFF;
331 
332 	return lux;
333 }
334 
335 static int cm32181_read_raw(struct iio_dev *indio_dev,
336 			    struct iio_chan_spec const *chan,
337 			    int *val, int *val2, long mask)
338 {
339 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
340 	int ret;
341 
342 	switch (mask) {
343 	case IIO_CHAN_INFO_PROCESSED:
344 		ret = cm32181_get_lux(cm32181);
345 		if (ret < 0)
346 			return ret;
347 		*val = ret;
348 		return IIO_VAL_INT;
349 	case IIO_CHAN_INFO_CALIBSCALE:
350 		*val = cm32181->calibscale;
351 		return IIO_VAL_INT;
352 	case IIO_CHAN_INFO_INT_TIME:
353 		*val = 0;
354 		ret = cm32181_read_als_it(cm32181, val2);
355 		return ret;
356 	}
357 
358 	return -EINVAL;
359 }
360 
361 static int cm32181_write_raw(struct iio_dev *indio_dev,
362 			     struct iio_chan_spec const *chan,
363 			     int val, int val2, long mask)
364 {
365 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
366 	int ret;
367 
368 	switch (mask) {
369 	case IIO_CHAN_INFO_CALIBSCALE:
370 		cm32181->calibscale = val;
371 		return 0;
372 	case IIO_CHAN_INFO_INT_TIME:
373 		ret = cm32181_write_als_it(cm32181, val2);
374 		return ret;
375 	}
376 
377 	return -EINVAL;
378 }
379 
380 /**
381  * cm32181_get_it_available() - Get available ALS IT value
382  * @dev:	pointer of struct device.
383  * @attr:	pointer of struct device_attribute.
384  * @buf:	pointer of return string buffer.
385  *
386  * Display the available integration time values by millisecond.
387  *
388  * Return: string length.
389  */
390 static ssize_t cm32181_get_it_available(struct device *dev,
391 			struct device_attribute *attr, char *buf)
392 {
393 	struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev));
394 	int i, n, len;
395 
396 	n = cm32181->num_als_it;
397 	for (i = 0, len = 0; i < n; i++)
398 		len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]);
399 	return len + sprintf(buf + len, "\n");
400 }
401 
402 static const struct iio_chan_spec cm32181_channels[] = {
403 	{
404 		.type = IIO_LIGHT,
405 		.info_mask_separate =
406 			BIT(IIO_CHAN_INFO_PROCESSED) |
407 			BIT(IIO_CHAN_INFO_CALIBSCALE) |
408 			BIT(IIO_CHAN_INFO_INT_TIME),
409 	}
410 };
411 
412 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
413 			S_IRUGO, cm32181_get_it_available, NULL, 0);
414 
415 static struct attribute *cm32181_attributes[] = {
416 	&iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
417 	NULL,
418 };
419 
420 static const struct attribute_group cm32181_attribute_group = {
421 	.attrs = cm32181_attributes
422 };
423 
424 static const struct iio_info cm32181_info = {
425 	.read_raw		= &cm32181_read_raw,
426 	.write_raw		= &cm32181_write_raw,
427 	.attrs			= &cm32181_attribute_group,
428 };
429 
430 static void cm32181_unregister_dummy_client(void *data)
431 {
432 	struct i2c_client *client = data;
433 
434 	/* Unregister the dummy client */
435 	i2c_unregister_device(client);
436 }
437 
438 static int cm32181_probe(struct i2c_client *client)
439 {
440 	struct device *dev = &client->dev;
441 	struct cm32181_chip *cm32181;
442 	struct iio_dev *indio_dev;
443 	int ret;
444 
445 	indio_dev = devm_iio_device_alloc(dev, sizeof(*cm32181));
446 	if (!indio_dev)
447 		return -ENOMEM;
448 
449 	i2c_set_clientdata(client, indio_dev);
450 
451 	/*
452 	 * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the
453 	 * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address.
454 	 * Detect this and take the following step to deal with it:
455 	 * 1. When a SMBus Alert capable sensor has an Alert asserted, it will
456 	 *    not respond on its actual I2C address. Read a byte from the ARA
457 	 *    to clear any pending Alerts.
458 	 * 2. Create a "dummy" client for the actual I2C address and
459 	 *    use that client to communicate with the sensor.
460 	 */
461 	if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {
462 		struct i2c_board_info board_info = { .type = "dummy" };
463 
464 		i2c_smbus_read_byte(client);
465 
466 		client = i2c_acpi_new_device(dev, 1, &board_info);
467 		if (IS_ERR(client))
468 			return PTR_ERR(client);
469 
470 		ret = devm_add_action_or_reset(dev, cm32181_unregister_dummy_client, client);
471 		if (ret)
472 			return ret;
473 	}
474 
475 	cm32181 = iio_priv(indio_dev);
476 	cm32181->client = client;
477 	cm32181->dev = dev;
478 
479 	mutex_init(&cm32181->lock);
480 	indio_dev->channels = cm32181_channels;
481 	indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
482 	indio_dev->info = &cm32181_info;
483 	indio_dev->name = dev_name(dev);
484 	indio_dev->modes = INDIO_DIRECT_MODE;
485 
486 	ret = cm32181_reg_init(cm32181);
487 	if (ret) {
488 		dev_err(dev, "%s: register init failed\n", __func__);
489 		return ret;
490 	}
491 
492 	ret = devm_iio_device_register(dev, indio_dev);
493 	if (ret) {
494 		dev_err(dev, "%s: register device failed\n", __func__);
495 		return ret;
496 	}
497 
498 	return 0;
499 }
500 
501 static int cm32181_suspend(struct device *dev)
502 {
503 	struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
504 	struct i2c_client *client = cm32181->client;
505 
506 	return i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
507 					 CM32181_CMD_ALS_DISABLE);
508 }
509 
510 static int cm32181_resume(struct device *dev)
511 {
512 	struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
513 	struct i2c_client *client = cm32181->client;
514 
515 	return i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
516 					 cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
517 }
518 
519 static DEFINE_SIMPLE_DEV_PM_OPS(cm32181_pm_ops, cm32181_suspend, cm32181_resume);
520 
521 static const struct of_device_id cm32181_of_match[] = {
522 	{ .compatible = "capella,cm3218" },
523 	{ .compatible = "capella,cm32181" },
524 	{ }
525 };
526 MODULE_DEVICE_TABLE(of, cm32181_of_match);
527 
528 #ifdef CONFIG_ACPI
529 static const struct acpi_device_id cm32181_acpi_match[] = {
530 	{ "CPLM3218", 0 },
531 	{ }
532 };
533 MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match);
534 #endif
535 
536 static struct i2c_driver cm32181_driver = {
537 	.driver = {
538 		.name	= "cm32181",
539 		.acpi_match_table = ACPI_PTR(cm32181_acpi_match),
540 		.of_match_table = cm32181_of_match,
541 		.pm = pm_sleep_ptr(&cm32181_pm_ops),
542 	},
543 	.probe		= cm32181_probe,
544 };
545 
546 module_i2c_driver(cm32181_driver);
547 
548 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
549 MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
550 MODULE_LICENSE("GPL");
551