xref: /openbmc/linux/drivers/iio/light/jsa1212.c (revision 60772e48)
1 /*
2  * JSA1212 Ambient Light & Proximity Sensor Driver
3  *
4  * Copyright (c) 2014, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * JSA1212 I2C slave address: 0x44(ADDR tied to GND), 0x45(ADDR tied to VDD)
16  *
17  * TODO: Interrupt support, thresholds, range support.
18  */
19 
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/mutex.h>
26 #include <linux/acpi.h>
27 #include <linux/regmap.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 
31 /* JSA1212 reg address */
32 #define JSA1212_CONF_REG		0x01
33 #define JSA1212_INT_REG			0x02
34 #define JSA1212_PXS_LT_REG		0x03
35 #define JSA1212_PXS_HT_REG		0x04
36 #define JSA1212_ALS_TH1_REG		0x05
37 #define JSA1212_ALS_TH2_REG		0x06
38 #define JSA1212_ALS_TH3_REG		0x07
39 #define JSA1212_PXS_DATA_REG		0x08
40 #define JSA1212_ALS_DT1_REG		0x09
41 #define JSA1212_ALS_DT2_REG		0x0A
42 #define JSA1212_ALS_RNG_REG		0x0B
43 #define JSA1212_MAX_REG			0x0C
44 
45 /* JSA1212 reg masks */
46 #define JSA1212_CONF_MASK		0xFF
47 #define JSA1212_INT_MASK		0xFF
48 #define JSA1212_PXS_LT_MASK		0xFF
49 #define JSA1212_PXS_HT_MASK		0xFF
50 #define JSA1212_ALS_TH1_MASK		0xFF
51 #define JSA1212_ALS_TH2_LT_MASK		0x0F
52 #define JSA1212_ALS_TH2_HT_MASK		0xF0
53 #define JSA1212_ALS_TH3_MASK		0xFF
54 #define JSA1212_PXS_DATA_MASK		0xFF
55 #define JSA1212_ALS_DATA_MASK		0x0FFF
56 #define JSA1212_ALS_DT1_MASK		0xFF
57 #define JSA1212_ALS_DT2_MASK		0x0F
58 #define JSA1212_ALS_RNG_MASK		0x07
59 
60 /* JSA1212 CONF REG bits */
61 #define JSA1212_CONF_PXS_MASK		0x80
62 #define JSA1212_CONF_PXS_ENABLE		0x80
63 #define JSA1212_CONF_PXS_DISABLE	0x00
64 #define JSA1212_CONF_ALS_MASK		0x04
65 #define JSA1212_CONF_ALS_ENABLE		0x04
66 #define JSA1212_CONF_ALS_DISABLE	0x00
67 #define JSA1212_CONF_IRDR_MASK		0x08
68 /* Proxmity sensing IRDR current sink settings */
69 #define JSA1212_CONF_IRDR_200MA		0x08
70 #define JSA1212_CONF_IRDR_100MA		0x00
71 #define JSA1212_CONF_PXS_SLP_MASK	0x70
72 #define JSA1212_CONF_PXS_SLP_0MS	0x70
73 #define JSA1212_CONF_PXS_SLP_12MS	0x60
74 #define JSA1212_CONF_PXS_SLP_50MS	0x50
75 #define JSA1212_CONF_PXS_SLP_75MS	0x40
76 #define JSA1212_CONF_PXS_SLP_100MS	0x30
77 #define JSA1212_CONF_PXS_SLP_200MS	0x20
78 #define JSA1212_CONF_PXS_SLP_400MS	0x10
79 #define JSA1212_CONF_PXS_SLP_800MS	0x00
80 
81 /* JSA1212 INT REG bits */
82 #define JSA1212_INT_CTRL_MASK		0x01
83 #define JSA1212_INT_CTRL_EITHER		0x00
84 #define JSA1212_INT_CTRL_BOTH		0x01
85 #define JSA1212_INT_ALS_PRST_MASK	0x06
86 #define JSA1212_INT_ALS_PRST_1CONV	0x00
87 #define JSA1212_INT_ALS_PRST_4CONV	0x02
88 #define JSA1212_INT_ALS_PRST_8CONV	0x04
89 #define JSA1212_INT_ALS_PRST_16CONV	0x06
90 #define JSA1212_INT_ALS_FLAG_MASK	0x08
91 #define JSA1212_INT_ALS_FLAG_CLR	0x00
92 #define JSA1212_INT_PXS_PRST_MASK	0x60
93 #define JSA1212_INT_PXS_PRST_1CONV	0x00
94 #define JSA1212_INT_PXS_PRST_4CONV	0x20
95 #define JSA1212_INT_PXS_PRST_8CONV	0x40
96 #define JSA1212_INT_PXS_PRST_16CONV	0x60
97 #define JSA1212_INT_PXS_FLAG_MASK	0x80
98 #define JSA1212_INT_PXS_FLAG_CLR	0x00
99 
100 /* JSA1212 ALS RNG REG bits */
101 #define JSA1212_ALS_RNG_0_2048		0x00
102 #define JSA1212_ALS_RNG_0_1024		0x01
103 #define JSA1212_ALS_RNG_0_512		0x02
104 #define JSA1212_ALS_RNG_0_256		0x03
105 #define JSA1212_ALS_RNG_0_128		0x04
106 
107 /* JSA1212 INT threshold range */
108 #define JSA1212_ALS_TH_MIN	0x0000
109 #define JSA1212_ALS_TH_MAX	0x0FFF
110 #define JSA1212_PXS_TH_MIN	0x00
111 #define JSA1212_PXS_TH_MAX	0xFF
112 
113 #define JSA1212_ALS_DELAY_MS	200
114 #define JSA1212_PXS_DELAY_MS	100
115 
116 #define JSA1212_DRIVER_NAME	"jsa1212"
117 #define JSA1212_REGMAP_NAME	"jsa1212_regmap"
118 
119 enum jsa1212_op_mode {
120 	JSA1212_OPMODE_ALS_EN,
121 	JSA1212_OPMODE_PXS_EN,
122 };
123 
124 struct jsa1212_data {
125 	struct i2c_client *client;
126 	struct mutex lock;
127 	u8 als_rng_idx;
128 	bool als_en; /* ALS enable status */
129 	bool pxs_en; /* proximity enable status */
130 	struct regmap *regmap;
131 };
132 
133 /* ALS range idx to val mapping */
134 static const int jsa1212_als_range_val[] = {2048, 1024, 512, 256, 128,
135 						128, 128, 128};
136 
137 /* Enables or disables ALS function based on status */
138 static int jsa1212_als_enable(struct jsa1212_data *data, u8 status)
139 {
140 	int ret;
141 
142 	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
143 				JSA1212_CONF_ALS_MASK,
144 				status);
145 	if (ret < 0)
146 		return ret;
147 
148 	data->als_en = !!status;
149 
150 	return 0;
151 }
152 
153 /* Enables or disables PXS function based on status */
154 static int jsa1212_pxs_enable(struct jsa1212_data *data, u8 status)
155 {
156 	int ret;
157 
158 	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
159 				JSA1212_CONF_PXS_MASK,
160 				status);
161 	if (ret < 0)
162 		return ret;
163 
164 	data->pxs_en = !!status;
165 
166 	return 0;
167 }
168 
169 static int jsa1212_read_als_data(struct jsa1212_data *data,
170 				unsigned int *val)
171 {
172 	int ret;
173 	__le16 als_data;
174 
175 	ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
176 	if (ret < 0)
177 		return ret;
178 
179 	/* Delay for data output */
180 	msleep(JSA1212_ALS_DELAY_MS);
181 
182 	/* Read 12 bit data */
183 	ret = regmap_bulk_read(data->regmap, JSA1212_ALS_DT1_REG, &als_data, 2);
184 	if (ret < 0) {
185 		dev_err(&data->client->dev, "als data read err\n");
186 		goto als_data_read_err;
187 	}
188 
189 	*val = le16_to_cpu(als_data);
190 
191 als_data_read_err:
192 	return jsa1212_als_enable(data, JSA1212_CONF_ALS_DISABLE);
193 }
194 
195 static int jsa1212_read_pxs_data(struct jsa1212_data *data,
196 				unsigned int *val)
197 {
198 	int ret;
199 	unsigned int pxs_data;
200 
201 	ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
202 	if (ret < 0)
203 		return ret;
204 
205 	/* Delay for data output */
206 	msleep(JSA1212_PXS_DELAY_MS);
207 
208 	/* Read out all data */
209 	ret = regmap_read(data->regmap, JSA1212_PXS_DATA_REG, &pxs_data);
210 	if (ret < 0) {
211 		dev_err(&data->client->dev, "pxs data read err\n");
212 		goto pxs_data_read_err;
213 	}
214 
215 	*val = pxs_data & JSA1212_PXS_DATA_MASK;
216 
217 pxs_data_read_err:
218 	return jsa1212_pxs_enable(data, JSA1212_CONF_PXS_DISABLE);
219 }
220 
221 static int jsa1212_read_raw(struct iio_dev *indio_dev,
222 				struct iio_chan_spec const *chan,
223 				int *val, int *val2, long mask)
224 {
225 	int ret;
226 	struct jsa1212_data *data = iio_priv(indio_dev);
227 
228 	switch (mask) {
229 	case IIO_CHAN_INFO_RAW:
230 		mutex_lock(&data->lock);
231 		switch (chan->type) {
232 		case IIO_LIGHT:
233 			ret = jsa1212_read_als_data(data, val);
234 			break;
235 		case IIO_PROXIMITY:
236 			ret = jsa1212_read_pxs_data(data, val);
237 			break;
238 		default:
239 			ret = -EINVAL;
240 			break;
241 		}
242 		mutex_unlock(&data->lock);
243 		return ret < 0 ? ret : IIO_VAL_INT;
244 	case IIO_CHAN_INFO_SCALE:
245 		switch (chan->type) {
246 		case IIO_LIGHT:
247 			*val = jsa1212_als_range_val[data->als_rng_idx];
248 			*val2 = BIT(12); /* Max 12 bit value */
249 			return IIO_VAL_FRACTIONAL;
250 		default:
251 			break;
252 		}
253 		break;
254 	default:
255 		break;
256 	}
257 
258 	return -EINVAL;
259 }
260 
261 static const struct iio_chan_spec jsa1212_channels[] = {
262 	{
263 		.type = IIO_LIGHT,
264 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
265 			BIT(IIO_CHAN_INFO_SCALE),
266 	},
267 	{
268 		.type = IIO_PROXIMITY,
269 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
270 	}
271 };
272 
273 static const struct iio_info jsa1212_info = {
274 	.read_raw		= &jsa1212_read_raw,
275 };
276 
277 static int jsa1212_chip_init(struct jsa1212_data *data)
278 {
279 	int ret;
280 
281 	ret = regmap_write(data->regmap, JSA1212_CONF_REG,
282 				(JSA1212_CONF_PXS_SLP_50MS |
283 				JSA1212_CONF_IRDR_200MA));
284 	if (ret < 0)
285 		return ret;
286 
287 	ret = regmap_write(data->regmap, JSA1212_INT_REG,
288 				JSA1212_INT_ALS_PRST_4CONV);
289 	if (ret < 0)
290 		return ret;
291 
292 	data->als_rng_idx = JSA1212_ALS_RNG_0_2048;
293 
294 	return 0;
295 }
296 
297 static bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
298 {
299 	switch (reg) {
300 	case JSA1212_PXS_DATA_REG:
301 	case JSA1212_ALS_DT1_REG:
302 	case JSA1212_ALS_DT2_REG:
303 	case JSA1212_INT_REG:
304 		return true;
305 	default:
306 		return false;
307 	}
308 }
309 
310 static const struct regmap_config jsa1212_regmap_config = {
311 	.name =  JSA1212_REGMAP_NAME,
312 	.reg_bits = 8,
313 	.val_bits = 8,
314 	.max_register = JSA1212_MAX_REG,
315 	.cache_type = REGCACHE_RBTREE,
316 	.volatile_reg = jsa1212_is_volatile_reg,
317 };
318 
319 static int jsa1212_probe(struct i2c_client *client,
320 			 const struct i2c_device_id *id)
321 {
322 	struct jsa1212_data *data;
323 	struct iio_dev *indio_dev;
324 	struct regmap *regmap;
325 	int ret;
326 
327 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
328 	if (!indio_dev)
329 		return -ENOMEM;
330 
331 	regmap = devm_regmap_init_i2c(client, &jsa1212_regmap_config);
332 	if (IS_ERR(regmap)) {
333 		dev_err(&client->dev, "Regmap initialization failed.\n");
334 		return PTR_ERR(regmap);
335 	}
336 
337 	data = iio_priv(indio_dev);
338 
339 	i2c_set_clientdata(client, indio_dev);
340 	data->client = client;
341 	data->regmap = regmap;
342 
343 	mutex_init(&data->lock);
344 
345 	ret = jsa1212_chip_init(data);
346 	if (ret < 0)
347 		return ret;
348 
349 	indio_dev->dev.parent = &client->dev;
350 	indio_dev->channels = jsa1212_channels;
351 	indio_dev->num_channels = ARRAY_SIZE(jsa1212_channels);
352 	indio_dev->name = JSA1212_DRIVER_NAME;
353 	indio_dev->modes = INDIO_DIRECT_MODE;
354 
355 	indio_dev->info = &jsa1212_info;
356 
357 	ret = iio_device_register(indio_dev);
358 	if (ret < 0)
359 		dev_err(&client->dev, "%s: register device failed\n", __func__);
360 
361 	return ret;
362 }
363 
364  /* power off the device */
365 static int jsa1212_power_off(struct jsa1212_data *data)
366 {
367 	int ret;
368 
369 	mutex_lock(&data->lock);
370 
371 	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
372 				JSA1212_CONF_ALS_MASK |
373 				JSA1212_CONF_PXS_MASK,
374 				JSA1212_CONF_ALS_DISABLE |
375 				JSA1212_CONF_PXS_DISABLE);
376 
377 	if (ret < 0)
378 		dev_err(&data->client->dev, "power off cmd failed\n");
379 
380 	mutex_unlock(&data->lock);
381 
382 	return ret;
383 }
384 
385 static int jsa1212_remove(struct i2c_client *client)
386 {
387 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
388 	struct jsa1212_data *data = iio_priv(indio_dev);
389 
390 	iio_device_unregister(indio_dev);
391 
392 	return jsa1212_power_off(data);
393 }
394 
395 #ifdef CONFIG_PM_SLEEP
396 static int jsa1212_suspend(struct device *dev)
397 {
398 	struct jsa1212_data *data;
399 
400 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
401 
402 	return jsa1212_power_off(data);
403 }
404 
405 static int jsa1212_resume(struct device *dev)
406 {
407 	int ret = 0;
408 	struct jsa1212_data *data;
409 
410 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
411 
412 	mutex_lock(&data->lock);
413 
414 	if (data->als_en) {
415 		ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
416 		if (ret < 0) {
417 			dev_err(dev, "als resume failed\n");
418 			goto unlock_and_ret;
419 		}
420 	}
421 
422 	if (data->pxs_en) {
423 		ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
424 		if (ret < 0)
425 			dev_err(dev, "pxs resume failed\n");
426 	}
427 
428 unlock_and_ret:
429 	mutex_unlock(&data->lock);
430 	return ret;
431 }
432 
433 static SIMPLE_DEV_PM_OPS(jsa1212_pm_ops, jsa1212_suspend, jsa1212_resume);
434 
435 #define JSA1212_PM_OPS (&jsa1212_pm_ops)
436 #else
437 #define JSA1212_PM_OPS NULL
438 #endif
439 
440 static const struct acpi_device_id jsa1212_acpi_match[] = {
441 	{"JSA1212", 0},
442 	{ },
443 };
444 MODULE_DEVICE_TABLE(acpi, jsa1212_acpi_match);
445 
446 static const struct i2c_device_id jsa1212_id[] = {
447 	{ JSA1212_DRIVER_NAME, 0 },
448 	{ }
449 };
450 MODULE_DEVICE_TABLE(i2c, jsa1212_id);
451 
452 static struct i2c_driver jsa1212_driver = {
453 	.driver = {
454 		.name	= JSA1212_DRIVER_NAME,
455 		.pm	= JSA1212_PM_OPS,
456 		.acpi_match_table = ACPI_PTR(jsa1212_acpi_match),
457 	},
458 	.probe		= jsa1212_probe,
459 	.remove		= jsa1212_remove,
460 	.id_table	= jsa1212_id,
461 };
462 module_i2c_driver(jsa1212_driver);
463 
464 MODULE_AUTHOR("Sathya Kuppuswamy <sathyanarayanan.kuppuswamy@linux.intel.com>");
465 MODULE_DESCRIPTION("JSA1212 proximity/ambient light sensor driver");
466 MODULE_LICENSE("GPL v2");
467