xref: /linux/drivers/iio/light/cm3232.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b84894c7SKevin Tsai /*
3b84894c7SKevin Tsai  * CM3232 Ambient Light Sensor
4b84894c7SKevin Tsai  *
5b84894c7SKevin Tsai  * Copyright (C) 2014-2015 Capella Microsystems Inc.
6b84894c7SKevin Tsai  * Author: Kevin Tsai <ktsai@capellamicro.com>
7b84894c7SKevin Tsai  *
8b84894c7SKevin Tsai  * IIO driver for CM3232 (7-bit I2C slave address 0x10).
9b84894c7SKevin Tsai  */
10b84894c7SKevin Tsai 
11b84894c7SKevin Tsai #include <linux/i2c.h>
12b84894c7SKevin Tsai #include <linux/module.h>
131de94b59SJonathan Cameron #include <linux/mod_devicetable.h>
14b84894c7SKevin Tsai #include <linux/iio/iio.h>
15b84894c7SKevin Tsai #include <linux/iio/sysfs.h>
16b84894c7SKevin Tsai #include <linux/init.h>
17b84894c7SKevin Tsai 
18b84894c7SKevin Tsai /* Registers Address */
19b84894c7SKevin Tsai #define CM3232_REG_ADDR_CMD		0x00
20b84894c7SKevin Tsai #define CM3232_REG_ADDR_ALS		0x50
21b84894c7SKevin Tsai #define CM3232_REG_ADDR_ID		0x53
22b84894c7SKevin Tsai 
23b84894c7SKevin Tsai #define CM3232_CMD_ALS_DISABLE		BIT(0)
24b84894c7SKevin Tsai 
25b84894c7SKevin Tsai #define CM3232_CMD_ALS_IT_SHIFT		2
26b84894c7SKevin Tsai #define CM3232_CMD_ALS_IT_MASK		(BIT(2) | BIT(3) | BIT(4))
27b84894c7SKevin Tsai #define CM3232_CMD_ALS_IT_DEFAULT	(0x01 << CM3232_CMD_ALS_IT_SHIFT)
28b84894c7SKevin Tsai 
29b84894c7SKevin Tsai #define CM3232_CMD_ALS_RESET		BIT(6)
30b84894c7SKevin Tsai 
31b84894c7SKevin Tsai #define CM3232_CMD_DEFAULT		CM3232_CMD_ALS_IT_DEFAULT
32b84894c7SKevin Tsai 
33b84894c7SKevin Tsai #define CM3232_HW_ID			0x32
34b84894c7SKevin Tsai #define CM3232_CALIBSCALE_DEFAULT	100000
35b84894c7SKevin Tsai #define CM3232_CALIBSCALE_RESOLUTION	100000
36b84894c7SKevin Tsai #define CM3232_MLUX_PER_LUX		1000
37b84894c7SKevin Tsai 
38b84894c7SKevin Tsai #define CM3232_MLUX_PER_BIT_DEFAULT	64
39b84894c7SKevin Tsai #define CM3232_MLUX_PER_BIT_BASE_IT	100000
40b84894c7SKevin Tsai 
41b84894c7SKevin Tsai static const struct {
42b84894c7SKevin Tsai 	int val;
43b84894c7SKevin Tsai 	int val2;
44b84894c7SKevin Tsai 	u8 it;
45b84894c7SKevin Tsai } cm3232_als_it_scales[] = {
46b84894c7SKevin Tsai 	{0, 100000, 0},	/* 0.100000 */
47b84894c7SKevin Tsai 	{0, 200000, 1},	/* 0.200000 */
48b84894c7SKevin Tsai 	{0, 400000, 2},	/* 0.400000 */
49b84894c7SKevin Tsai 	{0, 800000, 3},	/* 0.800000 */
50b84894c7SKevin Tsai 	{1, 600000, 4},	/* 1.600000 */
51b84894c7SKevin Tsai 	{3, 200000, 5},	/* 3.200000 */
52b84894c7SKevin Tsai };
53b84894c7SKevin Tsai 
54b84894c7SKevin Tsai struct cm3232_als_info {
55b84894c7SKevin Tsai 	u8 regs_cmd_default;
56b84894c7SKevin Tsai 	u8 hw_id;
57b84894c7SKevin Tsai 	int calibscale;
58b84894c7SKevin Tsai 	int mlux_per_bit;
59b84894c7SKevin Tsai 	int mlux_per_bit_base_it;
60b84894c7SKevin Tsai };
61b84894c7SKevin Tsai 
62b84894c7SKevin Tsai static struct cm3232_als_info cm3232_als_info_default = {
63b84894c7SKevin Tsai 	.regs_cmd_default = CM3232_CMD_DEFAULT,
64b84894c7SKevin Tsai 	.hw_id = CM3232_HW_ID,
65b84894c7SKevin Tsai 	.calibscale = CM3232_CALIBSCALE_DEFAULT,
66b84894c7SKevin Tsai 	.mlux_per_bit = CM3232_MLUX_PER_BIT_DEFAULT,
67b84894c7SKevin Tsai 	.mlux_per_bit_base_it = CM3232_MLUX_PER_BIT_BASE_IT,
68b84894c7SKevin Tsai };
69b84894c7SKevin Tsai 
70b84894c7SKevin Tsai struct cm3232_chip {
71b84894c7SKevin Tsai 	struct i2c_client *client;
72b84894c7SKevin Tsai 	struct cm3232_als_info *als_info;
73b84894c7SKevin Tsai 	u8 regs_cmd;
74b84894c7SKevin Tsai 	u16 regs_als;
75b84894c7SKevin Tsai };
76b84894c7SKevin Tsai 
77b84894c7SKevin Tsai /**
78b84894c7SKevin Tsai  * cm3232_reg_init() - Initialize CM3232
79b84894c7SKevin Tsai  * @chip:	pointer of struct cm3232_chip.
80b84894c7SKevin Tsai  *
81b84894c7SKevin Tsai  * Check and initialize CM3232 ambient light sensor.
82b84894c7SKevin Tsai  *
83b84894c7SKevin Tsai  * Return: 0 for success; otherwise for error code.
84b84894c7SKevin Tsai  */
cm3232_reg_init(struct cm3232_chip * chip)85b84894c7SKevin Tsai static int cm3232_reg_init(struct cm3232_chip *chip)
86b84894c7SKevin Tsai {
87b84894c7SKevin Tsai 	struct i2c_client *client = chip->client;
88b84894c7SKevin Tsai 	s32 ret;
89b84894c7SKevin Tsai 
90b84894c7SKevin Tsai 	chip->als_info = &cm3232_als_info_default;
91b84894c7SKevin Tsai 
92b84894c7SKevin Tsai 	/* Identify device */
93b84894c7SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ID);
94b84894c7SKevin Tsai 	if (ret < 0) {
95b84894c7SKevin Tsai 		dev_err(&chip->client->dev, "Error reading addr_id\n");
96b84894c7SKevin Tsai 		return ret;
97b84894c7SKevin Tsai 	}
98b84894c7SKevin Tsai 
99b84894c7SKevin Tsai 	if ((ret & 0xFF) != chip->als_info->hw_id)
100b84894c7SKevin Tsai 		return -ENODEV;
101b84894c7SKevin Tsai 
102b84894c7SKevin Tsai 	/* Disable and reset device */
103b84894c7SKevin Tsai 	chip->regs_cmd = CM3232_CMD_ALS_DISABLE | CM3232_CMD_ALS_RESET;
104b84894c7SKevin Tsai 	ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
105b84894c7SKevin Tsai 					chip->regs_cmd);
106b84894c7SKevin Tsai 	if (ret < 0) {
107b84894c7SKevin Tsai 		dev_err(&chip->client->dev, "Error writing reg_cmd\n");
108b84894c7SKevin Tsai 		return ret;
109b84894c7SKevin Tsai 	}
110b84894c7SKevin Tsai 
111b84894c7SKevin Tsai 	/* Register default value */
112b84894c7SKevin Tsai 	chip->regs_cmd = chip->als_info->regs_cmd_default;
113b84894c7SKevin Tsai 
114b84894c7SKevin Tsai 	/* Configure register */
115b84894c7SKevin Tsai 	ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
116b84894c7SKevin Tsai 					chip->regs_cmd);
117b84894c7SKevin Tsai 	if (ret < 0)
118b84894c7SKevin Tsai 		dev_err(&chip->client->dev, "Error writing reg_cmd\n");
119b84894c7SKevin Tsai 
120db4e5376SPan Bian 	return ret;
121b84894c7SKevin Tsai }
122b84894c7SKevin Tsai 
123b84894c7SKevin Tsai /**
124b84894c7SKevin Tsai  *  cm3232_read_als_it() - Get sensor integration time
125b84894c7SKevin Tsai  *  @chip:	pointer of struct cm3232_chip
126b84894c7SKevin Tsai  *  @val:	pointer of int to load the integration (sec).
127b84894c7SKevin Tsai  *  @val2:	pointer of int to load the integration time (microsecond).
128b84894c7SKevin Tsai  *
129b84894c7SKevin Tsai  *  Report the current integration time.
130b84894c7SKevin Tsai  *
131b84894c7SKevin Tsai  *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
132b84894c7SKevin Tsai  */
cm3232_read_als_it(struct cm3232_chip * chip,int * val,int * val2)133b84894c7SKevin Tsai static int cm3232_read_als_it(struct cm3232_chip *chip, int *val, int *val2)
134b84894c7SKevin Tsai {
135b84894c7SKevin Tsai 	u16 als_it;
136b84894c7SKevin Tsai 	int i;
137b84894c7SKevin Tsai 
138b84894c7SKevin Tsai 	als_it = chip->regs_cmd;
139b84894c7SKevin Tsai 	als_it &= CM3232_CMD_ALS_IT_MASK;
140b84894c7SKevin Tsai 	als_it >>= CM3232_CMD_ALS_IT_SHIFT;
141b84894c7SKevin Tsai 	for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
142b84894c7SKevin Tsai 		if (als_it == cm3232_als_it_scales[i].it) {
143b84894c7SKevin Tsai 			*val = cm3232_als_it_scales[i].val;
144b84894c7SKevin Tsai 			*val2 = cm3232_als_it_scales[i].val2;
145b84894c7SKevin Tsai 			return IIO_VAL_INT_PLUS_MICRO;
146b84894c7SKevin Tsai 		}
147b84894c7SKevin Tsai 	}
148b84894c7SKevin Tsai 
149b84894c7SKevin Tsai 	return -EINVAL;
150b84894c7SKevin Tsai }
151b84894c7SKevin Tsai 
152b84894c7SKevin Tsai /**
153b84894c7SKevin Tsai  * cm3232_write_als_it() - Write sensor integration time
154b84894c7SKevin Tsai  * @chip:	pointer of struct cm3232_chip.
155b84894c7SKevin Tsai  * @val:	integration time in second.
156b84894c7SKevin Tsai  * @val2:	integration time in microsecond.
157b84894c7SKevin Tsai  *
158b84894c7SKevin Tsai  * Convert integration time to sensor value.
159b84894c7SKevin Tsai  *
160b84894c7SKevin Tsai  * Return: i2c_smbus_write_byte_data command return value.
161b84894c7SKevin Tsai  */
cm3232_write_als_it(struct cm3232_chip * chip,int val,int val2)162b84894c7SKevin Tsai static int cm3232_write_als_it(struct cm3232_chip *chip, int val, int val2)
163b84894c7SKevin Tsai {
164b84894c7SKevin Tsai 	struct i2c_client *client = chip->client;
165b84894c7SKevin Tsai 	u16 als_it, cmd;
166b84894c7SKevin Tsai 	int i;
167b84894c7SKevin Tsai 	s32 ret;
168b84894c7SKevin Tsai 
169b84894c7SKevin Tsai 	for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
170b84894c7SKevin Tsai 		if (val == cm3232_als_it_scales[i].val &&
171b84894c7SKevin Tsai 			val2 == cm3232_als_it_scales[i].val2) {
172b84894c7SKevin Tsai 
173b84894c7SKevin Tsai 			als_it = cm3232_als_it_scales[i].it;
174b84894c7SKevin Tsai 			als_it <<= CM3232_CMD_ALS_IT_SHIFT;
175b84894c7SKevin Tsai 
176b84894c7SKevin Tsai 			cmd = chip->regs_cmd & ~CM3232_CMD_ALS_IT_MASK;
177b84894c7SKevin Tsai 			cmd |= als_it;
178b84894c7SKevin Tsai 			ret = i2c_smbus_write_byte_data(client,
179b84894c7SKevin Tsai 							CM3232_REG_ADDR_CMD,
180b84894c7SKevin Tsai 							cmd);
181b84894c7SKevin Tsai 			if (ret < 0)
182b84894c7SKevin Tsai 				return ret;
183b84894c7SKevin Tsai 			chip->regs_cmd = cmd;
184b84894c7SKevin Tsai 			return 0;
185b84894c7SKevin Tsai 		}
186b84894c7SKevin Tsai 	}
187b84894c7SKevin Tsai 	return -EINVAL;
188b84894c7SKevin Tsai }
189b84894c7SKevin Tsai 
190b84894c7SKevin Tsai /**
191b84894c7SKevin Tsai  * cm3232_get_lux() - report current lux value
192b84894c7SKevin Tsai  * @chip:	pointer of struct cm3232_chip.
193b84894c7SKevin Tsai  *
194b84894c7SKevin Tsai  * Convert sensor data to lux.  It depends on integration
195b84894c7SKevin Tsai  * time and calibscale variable.
196b84894c7SKevin Tsai  *
197b84894c7SKevin Tsai  * Return: Zero or positive value is lux, otherwise error code.
198b84894c7SKevin Tsai  */
cm3232_get_lux(struct cm3232_chip * chip)199b84894c7SKevin Tsai static int cm3232_get_lux(struct cm3232_chip *chip)
200b84894c7SKevin Tsai {
201b84894c7SKevin Tsai 	struct i2c_client *client = chip->client;
202b84894c7SKevin Tsai 	struct cm3232_als_info *als_info = chip->als_info;
203b84894c7SKevin Tsai 	int ret;
204b84894c7SKevin Tsai 	int val, val2;
205b84894c7SKevin Tsai 	int als_it;
206b84894c7SKevin Tsai 	u64 lux;
207b84894c7SKevin Tsai 
208b84894c7SKevin Tsai 	/* Calculate mlux per bit based on als_it */
209b84894c7SKevin Tsai 	ret = cm3232_read_als_it(chip, &val, &val2);
210b84894c7SKevin Tsai 	if (ret < 0)
211b84894c7SKevin Tsai 		return -EINVAL;
212b84894c7SKevin Tsai 	als_it = val * 1000000 + val2;
213b84894c7SKevin Tsai 	lux = (__force u64)als_info->mlux_per_bit;
214b84894c7SKevin Tsai 	lux *= als_info->mlux_per_bit_base_it;
215b84894c7SKevin Tsai 	lux = div_u64(lux, als_it);
216b84894c7SKevin Tsai 
217b84894c7SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ALS);
218b84894c7SKevin Tsai 	if (ret < 0) {
219b84894c7SKevin Tsai 		dev_err(&client->dev, "Error reading reg_addr_als\n");
220b84894c7SKevin Tsai 		return ret;
221b84894c7SKevin Tsai 	}
222b84894c7SKevin Tsai 
223b84894c7SKevin Tsai 	chip->regs_als = (u16)ret;
224b84894c7SKevin Tsai 	lux *= chip->regs_als;
225b84894c7SKevin Tsai 	lux *= als_info->calibscale;
226b84894c7SKevin Tsai 	lux = div_u64(lux, CM3232_CALIBSCALE_RESOLUTION);
227b84894c7SKevin Tsai 	lux = div_u64(lux, CM3232_MLUX_PER_LUX);
228b84894c7SKevin Tsai 
229b84894c7SKevin Tsai 	if (lux > 0xFFFF)
230b84894c7SKevin Tsai 		lux = 0xFFFF;
231b84894c7SKevin Tsai 
232b84894c7SKevin Tsai 	return (int)lux;
233b84894c7SKevin Tsai }
234b84894c7SKevin Tsai 
cm3232_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)235b84894c7SKevin Tsai static int cm3232_read_raw(struct iio_dev *indio_dev,
236b84894c7SKevin Tsai 			struct iio_chan_spec const *chan,
237b84894c7SKevin Tsai 			int *val, int *val2, long mask)
238b84894c7SKevin Tsai {
239b84894c7SKevin Tsai 	struct cm3232_chip *chip = iio_priv(indio_dev);
240b84894c7SKevin Tsai 	struct cm3232_als_info *als_info = chip->als_info;
241b84894c7SKevin Tsai 	int ret;
242b84894c7SKevin Tsai 
243b84894c7SKevin Tsai 	switch (mask) {
244b84894c7SKevin Tsai 	case IIO_CHAN_INFO_PROCESSED:
245b84894c7SKevin Tsai 		ret = cm3232_get_lux(chip);
246b84894c7SKevin Tsai 		if (ret < 0)
247b84894c7SKevin Tsai 			return ret;
248b84894c7SKevin Tsai 		*val = ret;
249b84894c7SKevin Tsai 		return IIO_VAL_INT;
250b84894c7SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
251b84894c7SKevin Tsai 		*val = als_info->calibscale;
252b84894c7SKevin Tsai 		return IIO_VAL_INT;
253b84894c7SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
254b84894c7SKevin Tsai 		return cm3232_read_als_it(chip, val, val2);
255b84894c7SKevin Tsai 	}
256b84894c7SKevin Tsai 
257b84894c7SKevin Tsai 	return -EINVAL;
258b84894c7SKevin Tsai }
259b84894c7SKevin Tsai 
cm3232_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)260b84894c7SKevin Tsai static int cm3232_write_raw(struct iio_dev *indio_dev,
261b84894c7SKevin Tsai 			struct iio_chan_spec const *chan,
262b84894c7SKevin Tsai 			int val, int val2, long mask)
263b84894c7SKevin Tsai {
264b84894c7SKevin Tsai 	struct cm3232_chip *chip = iio_priv(indio_dev);
265b84894c7SKevin Tsai 	struct cm3232_als_info *als_info = chip->als_info;
266b84894c7SKevin Tsai 
267b84894c7SKevin Tsai 	switch (mask) {
268b84894c7SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
269b84894c7SKevin Tsai 		als_info->calibscale = val;
270b84894c7SKevin Tsai 		return 0;
271b84894c7SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
272b84894c7SKevin Tsai 		return cm3232_write_als_it(chip, val, val2);
273b84894c7SKevin Tsai 	}
274b84894c7SKevin Tsai 
275b84894c7SKevin Tsai 	return -EINVAL;
276b84894c7SKevin Tsai }
277b84894c7SKevin Tsai 
278b84894c7SKevin Tsai /**
279b84894c7SKevin Tsai  * cm3232_get_it_available() - Get available ALS IT value
280b84894c7SKevin Tsai  * @dev:	pointer of struct device.
281b84894c7SKevin Tsai  * @attr:	pointer of struct device_attribute.
282b84894c7SKevin Tsai  * @buf:	pointer of return string buffer.
283b84894c7SKevin Tsai  *
284b84894c7SKevin Tsai  * Display the available integration time in second.
285b84894c7SKevin Tsai  *
286b84894c7SKevin Tsai  * Return: string length.
287b84894c7SKevin Tsai  */
cm3232_get_it_available(struct device * dev,struct device_attribute * attr,char * buf)288b84894c7SKevin Tsai static ssize_t cm3232_get_it_available(struct device *dev,
289b84894c7SKevin Tsai 			struct device_attribute *attr, char *buf)
290b84894c7SKevin Tsai {
291b84894c7SKevin Tsai 	int i, len;
292b84894c7SKevin Tsai 
293b84894c7SKevin Tsai 	for (i = 0, len = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++)
294b84894c7SKevin Tsai 		len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ",
295b84894c7SKevin Tsai 			cm3232_als_it_scales[i].val,
296b84894c7SKevin Tsai 			cm3232_als_it_scales[i].val2);
297b84894c7SKevin Tsai 	return len + scnprintf(buf + len, PAGE_SIZE - len, "\n");
298b84894c7SKevin Tsai }
299b84894c7SKevin Tsai 
300b84894c7SKevin Tsai static const struct iio_chan_spec cm3232_channels[] = {
301b84894c7SKevin Tsai 	{
302b84894c7SKevin Tsai 		.type = IIO_LIGHT,
303b84894c7SKevin Tsai 		.info_mask_separate =
304b84894c7SKevin Tsai 			BIT(IIO_CHAN_INFO_PROCESSED) |
305b84894c7SKevin Tsai 			BIT(IIO_CHAN_INFO_CALIBSCALE) |
306b84894c7SKevin Tsai 			BIT(IIO_CHAN_INFO_INT_TIME),
307b84894c7SKevin Tsai 	}
308b84894c7SKevin Tsai };
309b84894c7SKevin Tsai 
310b84894c7SKevin Tsai static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
311b84894c7SKevin Tsai 			S_IRUGO, cm3232_get_it_available, NULL, 0);
312b84894c7SKevin Tsai 
313b84894c7SKevin Tsai static struct attribute *cm3232_attributes[] = {
314b84894c7SKevin Tsai 	&iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
315b84894c7SKevin Tsai 	NULL,
316b84894c7SKevin Tsai };
317b84894c7SKevin Tsai 
318b84894c7SKevin Tsai static const struct attribute_group cm3232_attribute_group = {
319b84894c7SKevin Tsai 	.attrs = cm3232_attributes
320b84894c7SKevin Tsai };
321b84894c7SKevin Tsai 
322b84894c7SKevin Tsai static const struct iio_info cm3232_info = {
323b84894c7SKevin Tsai 	.read_raw		= &cm3232_read_raw,
324b84894c7SKevin Tsai 	.write_raw		= &cm3232_write_raw,
325b84894c7SKevin Tsai 	.attrs			= &cm3232_attribute_group,
326b84894c7SKevin Tsai };
327b84894c7SKevin Tsai 
cm3232_probe(struct i2c_client * client)328f8232aadSUwe Kleine-König static int cm3232_probe(struct i2c_client *client)
329b84894c7SKevin Tsai {
330f8232aadSUwe Kleine-König 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
331b84894c7SKevin Tsai 	struct cm3232_chip *chip;
332b84894c7SKevin Tsai 	struct iio_dev *indio_dev;
333b84894c7SKevin Tsai 	int ret;
334b84894c7SKevin Tsai 
335b84894c7SKevin Tsai 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
336b84894c7SKevin Tsai 	if (!indio_dev)
337b84894c7SKevin Tsai 		return -ENOMEM;
338b84894c7SKevin Tsai 
339b84894c7SKevin Tsai 	chip = iio_priv(indio_dev);
340b84894c7SKevin Tsai 	i2c_set_clientdata(client, indio_dev);
341b84894c7SKevin Tsai 	chip->client = client;
342b84894c7SKevin Tsai 
343b84894c7SKevin Tsai 	indio_dev->channels = cm3232_channels;
344b84894c7SKevin Tsai 	indio_dev->num_channels = ARRAY_SIZE(cm3232_channels);
345b84894c7SKevin Tsai 	indio_dev->info = &cm3232_info;
346b84894c7SKevin Tsai 	indio_dev->name = id->name;
347b84894c7SKevin Tsai 	indio_dev->modes = INDIO_DIRECT_MODE;
348b84894c7SKevin Tsai 
349b84894c7SKevin Tsai 	ret = cm3232_reg_init(chip);
350b84894c7SKevin Tsai 	if (ret) {
351b84894c7SKevin Tsai 		dev_err(&client->dev,
352b84894c7SKevin Tsai 			"%s: register init failed\n",
353b84894c7SKevin Tsai 			__func__);
354b84894c7SKevin Tsai 		return ret;
355b84894c7SKevin Tsai 	}
356b84894c7SKevin Tsai 
357b84894c7SKevin Tsai 	return iio_device_register(indio_dev);
358b84894c7SKevin Tsai }
359b84894c7SKevin Tsai 
cm3232_remove(struct i2c_client * client)360ed5c2f5fSUwe Kleine-König static void cm3232_remove(struct i2c_client *client)
361b84894c7SKevin Tsai {
362b84894c7SKevin Tsai 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
363b84894c7SKevin Tsai 
364b84894c7SKevin Tsai 	i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
365b84894c7SKevin Tsai 		CM3232_CMD_ALS_DISABLE);
366b84894c7SKevin Tsai 
367b84894c7SKevin Tsai 	iio_device_unregister(indio_dev);
368b84894c7SKevin Tsai }
369b84894c7SKevin Tsai 
370b84894c7SKevin Tsai static const struct i2c_device_id cm3232_id[] = {
371*4391affaSUwe Kleine-König 	{ "cm3232" },
372b84894c7SKevin Tsai 	{}
373b84894c7SKevin Tsai };
374b84894c7SKevin Tsai 
cm3232_suspend(struct device * dev)3751ec28ce6SKevin Tsai static int cm3232_suspend(struct device *dev)
3761ec28ce6SKevin Tsai {
3771ec28ce6SKevin Tsai 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
3781ec28ce6SKevin Tsai 	struct cm3232_chip *chip = iio_priv(indio_dev);
3791ec28ce6SKevin Tsai 	struct i2c_client *client = chip->client;
3801ec28ce6SKevin Tsai 	int ret;
3811ec28ce6SKevin Tsai 
3821ec28ce6SKevin Tsai 	chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
3831ec28ce6SKevin Tsai 	ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
3841ec28ce6SKevin Tsai 					chip->regs_cmd);
3851ec28ce6SKevin Tsai 
3861ec28ce6SKevin Tsai 	return ret;
3871ec28ce6SKevin Tsai }
3881ec28ce6SKevin Tsai 
cm3232_resume(struct device * dev)3891ec28ce6SKevin Tsai static int cm3232_resume(struct device *dev)
3901ec28ce6SKevin Tsai {
3911ec28ce6SKevin Tsai 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
3921ec28ce6SKevin Tsai 	struct cm3232_chip *chip = iio_priv(indio_dev);
3931ec28ce6SKevin Tsai 	struct i2c_client *client = chip->client;
3941ec28ce6SKevin Tsai 	int ret;
3951ec28ce6SKevin Tsai 
3961ec28ce6SKevin Tsai 	chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
3971ec28ce6SKevin Tsai 	ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
3981ec28ce6SKevin Tsai 					chip->regs_cmd | CM3232_CMD_ALS_RESET);
3991ec28ce6SKevin Tsai 
4001ec28ce6SKevin Tsai 	return ret;
4011ec28ce6SKevin Tsai }
4021ec28ce6SKevin Tsai 
4035f3521b8SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(cm3232_pm_ops, cm3232_suspend, cm3232_resume);
4041ec28ce6SKevin Tsai 
405b84894c7SKevin Tsai MODULE_DEVICE_TABLE(i2c, cm3232_id);
406b84894c7SKevin Tsai 
407b84894c7SKevin Tsai static const struct of_device_id cm3232_of_match[] = {
408b84894c7SKevin Tsai 	{.compatible = "capella,cm3232"},
409b84894c7SKevin Tsai 	{}
410b84894c7SKevin Tsai };
411119c4fceSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, cm3232_of_match);
412b84894c7SKevin Tsai 
413b84894c7SKevin Tsai static struct i2c_driver cm3232_driver = {
414b84894c7SKevin Tsai 	.driver = {
415b84894c7SKevin Tsai 		.name	= "cm3232",
4161de94b59SJonathan Cameron 		.of_match_table = cm3232_of_match,
4175f3521b8SJonathan Cameron 		.pm	= pm_sleep_ptr(&cm3232_pm_ops),
418b84894c7SKevin Tsai 	},
419b84894c7SKevin Tsai 	.id_table	= cm3232_id,
4207cf15f42SUwe Kleine-König 	.probe		= cm3232_probe,
421b84894c7SKevin Tsai 	.remove		= cm3232_remove,
422b84894c7SKevin Tsai };
423b84894c7SKevin Tsai 
424b84894c7SKevin Tsai module_i2c_driver(cm3232_driver);
425b84894c7SKevin Tsai 
426b84894c7SKevin Tsai MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
427b84894c7SKevin Tsai MODULE_DESCRIPTION("CM3232 ambient light sensor driver");
428b84894c7SKevin Tsai MODULE_LICENSE("GPL");
429