Linux Audio

Check our new training course

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