summaryrefslogtreecommitdiffstats
path: root/drivers/iio/light/jsa1212.c
blob: e8a8931b4f50d0a2c1db1959a05b6722b2621d25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * JSA1212 Ambient Light & Proximity Sensor Driver
 *
 * Copyright (c) 2014, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * JSA1212 I2C slave address: 0x44(ADDR tied to GND), 0x45(ADDR tied to VDD)
 *
 * TODO: Interrupt support, thresholds, range support.
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/acpi.h>
#include <linux/regmap.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>

/* JSA1212 reg address */
#define JSA1212_CONF_REG		0x01
#define JSA1212_INT_REG			0x02
#define JSA1212_PXS_LT_REG		0x03
#define JSA1212_PXS_HT_REG		0x04
#define JSA1212_ALS_TH1_REG		0x05
#define JSA1212_ALS_TH2_REG		0x06
#define JSA1212_ALS_TH3_REG		0x07
#define JSA1212_PXS_DATA_REG		0x08
#define JSA1212_ALS_DT1_REG		0x09
#define JSA1212_ALS_DT2_REG		0x0A
#define JSA1212_ALS_RNG_REG		0x0B
#define JSA1212_MAX_REG			0x0C

/* JSA1212 reg masks */
#define JSA1212_CONF_MASK		0xFF
#define JSA1212_INT_MASK		0xFF
#define JSA1212_PXS_LT_MASK		0xFF
#define JSA1212_PXS_HT_MASK		0xFF
#define JSA1212_ALS_TH1_MASK		0xFF
#define JSA1212_ALS_TH2_LT_MASK		0x0F
#define JSA1212_ALS_TH2_HT_MASK		0xF0
#define JSA1212_ALS_TH3_MASK		0xFF
#define JSA1212_PXS_DATA_MASK		0xFF
#define JSA1212_ALS_DATA_MASK		0x0FFF
#define JSA1212_ALS_DT1_MASK		0xFF
#define JSA1212_ALS_DT2_MASK		0x0F
#define JSA1212_ALS_RNG_MASK		0x07

/* JSA1212 CONF REG bits */
#define JSA1212_CONF_PXS_MASK		0x80
#define JSA1212_CONF_PXS_ENABLE		0x80
#define JSA1212_CONF_PXS_DISABLE	0x00
#define JSA1212_CONF_ALS_MASK		0x04
#define JSA1212_CONF_ALS_ENABLE		0x04
#define JSA1212_CONF_ALS_DISABLE	0x00
#define JSA1212_CONF_IRDR_MASK		0x08
/* Proxmity sensing IRDR current sink settings */
#define JSA1212_CONF_IRDR_200MA		0x08
#define JSA1212_CONF_IRDR_100MA		0x00
#define JSA1212_CONF_PXS_SLP_MASK	0x70
#define JSA1212_CONF_PXS_SLP_0MS	0x70
#define JSA1212_CONF_PXS_SLP_12MS	0x60
#define JSA1212_CONF_PXS_SLP_50MS	0x50
#define JSA1212_CONF_PXS_SLP_75MS	0x40
#define JSA1212_CONF_PXS_SLP_100MS	0x30
#define JSA1212_CONF_PXS_SLP_200MS	0x20
#define JSA1212_CONF_PXS_SLP_400MS	0x10
#define JSA1212_CONF_PXS_SLP_800MS	0x00

/* JSA1212 INT REG bits */
#define JSA1212_INT_CTRL_MASK		0x01
#define JSA1212_INT_CTRL_EITHER		0x00
#define JSA1212_INT_CTRL_BOTH		0x01
#define JSA1212_INT_ALS_PRST_MASK	0x06
#define JSA1212_INT_ALS_PRST_1CONV	0x00
#define JSA1212_INT_ALS_PRST_4CONV	0x02
#define JSA1212_INT_ALS_PRST_8CONV	0x04
#define JSA1212_INT_ALS_PRST_16CONV	0x06
#define JSA1212_INT_ALS_FLAG_MASK	0x08
#define JSA1212_INT_ALS_FLAG_CLR	0x00
#define JSA1212_INT_PXS_PRST_MASK	0x60
#define JSA1212_INT_PXS_PRST_1CONV	0x00
#define JSA1212_INT_PXS_PRST_4CONV	0x20
#define JSA1212_INT_PXS_PRST_8CONV	0x40
#define JSA1212_INT_PXS_PRST_16CONV	0x60
#define JSA1212_INT_PXS_FLAG_MASK	0x80
#define JSA1212_INT_PXS_FLAG_CLR	0x00

/* JSA1212 ALS RNG REG bits */
#define JSA1212_ALS_RNG_0_2048		0x00
#define JSA1212_ALS_RNG_0_1024		0x01
#define JSA1212_ALS_RNG_0_512		0x02
#define JSA1212_ALS_RNG_0_256		0x03
#define JSA1212_ALS_RNG_0_128		0x04

/* JSA1212 INT threshold range */
#define JSA1212_ALS_TH_MIN	0x0000
#define JSA1212_ALS_TH_MAX	0x0FFF
#define JSA1212_PXS_TH_MIN	0x00
#define JSA1212_PXS_TH_MAX	0xFF

#define JSA1212_ALS_DELAY_MS	200
#define JSA1212_PXS_DELAY_MS	100

#define JSA1212_DRIVER_NAME	"jsa1212"
#define JSA1212_REGMAP_NAME	"jsa1212_regmap"

enum jsa1212_op_mode {
	JSA1212_OPMODE_ALS_EN,
	JSA1212_OPMODE_PXS_EN,
};

struct jsa1212_data {
	struct i2c_client *client;
	struct mutex lock;
	u8 als_rng_idx;
	bool als_en; /* ALS enable status */
	bool pxs_en; /* proximity enable status */
	struct regmap *regmap;
};

/* ALS range idx to val mapping */
static const int jsa1212_als_range_val[] = {2048, 1024, 512, 256, 128,
						128, 128, 128};

/* Enables or disables ALS function based on status */
static int jsa1212_als_enable(struct jsa1212_data *data, u8 status)
{
	int ret;

	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
				JSA1212_CONF_ALS_MASK,
				status);
	if (ret < 0)
		return ret;

	data->als_en = !!status;

	return 0;
}

/* Enables or disables PXS function based on status */
static int jsa1212_pxs_enable(struct jsa1212_data *data, u8 status)
{
	int ret;

	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
				JSA1212_CONF_PXS_MASK,
				status);
	if (ret < 0)
		return ret;

	data->pxs_en = !!status;

	return 0;
}

static int jsa1212_read_als_data(struct jsa1212_data *data,
				unsigned int *val)
{
	int ret;
	__le16 als_data;

	ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
	if (ret < 0)
		return ret;

	/* Delay for data output */
	msleep(JSA1212_ALS_DELAY_MS);

	/* Read 12 bit data */
	ret = regmap_bulk_read(data->regmap, JSA1212_ALS_DT1_REG, &als_data, 2);
	if (ret < 0) {
		dev_err(&data->client->dev, "als data read err\n");
		goto als_data_read_err;
	}

	*val = le16_to_cpu(als_data);

als_data_read_err:
	return jsa1212_als_enable(data, JSA1212_CONF_ALS_DISABLE);
}

static int jsa1212_read_pxs_data(struct jsa1212_data *data,
				unsigned int *val)
{
	int ret;
	unsigned int pxs_data;

	ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
	if (ret < 0)
		return ret;

	/* Delay for data output */
	msleep(JSA1212_PXS_DELAY_MS);

	/* Read out all data */
	ret = regmap_read(data->regmap, JSA1212_PXS_DATA_REG, &pxs_data);
	if (ret < 0) {
		dev_err(&data->client->dev, "pxs data read err\n");
		goto pxs_data_read_err;
	}

	*val = pxs_data & JSA1212_PXS_DATA_MASK;

pxs_data_read_err:
	return jsa1212_pxs_enable(data, JSA1212_CONF_PXS_DISABLE);
}

static int jsa1212_read_raw(struct iio_dev *indio_dev,
				struct iio_chan_spec const *chan,
				int *val, int *val2, long mask)
{
	int ret;
	struct jsa1212_data *data = iio_priv(indio_dev);

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		mutex_lock(&data->lock);
		switch (chan->type) {
		case IIO_LIGHT:
			ret = jsa1212_read_als_data(data, val);
			break;
		case IIO_PROXIMITY:
			ret = jsa1212_read_pxs_data(data, val);
			break;
		default:
			ret = -EINVAL;
			break;
		}
		mutex_unlock(&data->lock);
		return ret < 0 ? ret : IIO_VAL_INT;
	case IIO_CHAN_INFO_SCALE:
		switch (chan->type) {
		case IIO_LIGHT:
			*val = jsa1212_als_range_val[data->als_rng_idx];
			*val2 = BIT(12); /* Max 12 bit value */
			return IIO_VAL_FRACTIONAL;
		default:
			break;
		}
		break;
	default:
		break;
	}

	return -EINVAL;
}

static const struct iio_chan_spec jsa1212_channels[] = {
	{
		.type = IIO_LIGHT,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
			BIT(IIO_CHAN_INFO_SCALE),
	},
	{
		.type = IIO_PROXIMITY,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
	}
};

static const struct iio_info jsa1212_info = {
	.driver_module		= THIS_MODULE,
	.read_raw		= &jsa1212_read_raw,
};

static int jsa1212_chip_init(struct jsa1212_data *data)
{
	int ret;

	ret = regmap_write(data->regmap, JSA1212_CONF_REG,
				(JSA1212_CONF_PXS_SLP_50MS |
				JSA1212_CONF_IRDR_200MA));
	if (ret < 0)
		return ret;

	ret = regmap_write(data->regmap, JSA1212_INT_REG,
				JSA1212_INT_ALS_PRST_4CONV);
	if (ret < 0)
		return ret;

	data->als_rng_idx = JSA1212_ALS_RNG_0_2048;

	return 0;
}

static bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case JSA1212_PXS_DATA_REG:
	case JSA1212_ALS_DT1_REG:
	case JSA1212_ALS_DT2_REG:
	case JSA1212_INT_REG:
		return true;
	default:
		return false;
	}
}

static const struct regmap_config jsa1212_regmap_config = {
	.name =  JSA1212_REGMAP_NAME,
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = JSA1212_MAX_REG,
	.cache_type = REGCACHE_RBTREE,
	.volatile_reg = jsa1212_is_volatile_reg,
};

static int jsa1212_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct jsa1212_data *data;
	struct iio_dev *indio_dev;
	struct regmap *regmap;
	int ret;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
	if (!indio_dev)
		return -ENOMEM;

	regmap = devm_regmap_init_i2c(client, &jsa1212_regmap_config);
	if (IS_ERR(regmap)) {
		dev_err(&client->dev, "Regmap initialization failed.\n");
		return PTR_ERR(regmap);
	}

	data = iio_priv(indio_dev);

	i2c_set_clientdata(client, indio_dev);
	data->client = client;
	data->regmap = regmap;

	mutex_init(&data->lock);

	ret = jsa1212_chip_init(data);
	if (ret < 0)
		return ret;

	indio_dev->dev.parent = &client->dev;
	indio_dev->channels = jsa1212_channels;
	indio_dev->num_channels = ARRAY_SIZE(jsa1212_channels);
	indio_dev->name = JSA1212_DRIVER_NAME;
	indio_dev->modes = INDIO_DIRECT_MODE;

	indio_dev->info = &jsa1212_info;

	ret = iio_device_register(indio_dev);
	if (ret < 0)
		dev_err(&client->dev, "%s: register device failed\n", __func__);

	return ret;
}

 /* power off the device */
static int jsa1212_power_off(struct jsa1212_data *data)
{
	int ret;

	mutex_lock(&data->lock);

	ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
				JSA1212_CONF_ALS_MASK |
				JSA1212_CONF_PXS_MASK,
				JSA1212_CONF_ALS_DISABLE |
				JSA1212_CONF_PXS_DISABLE);

	if (ret < 0)
		dev_err(&data->client->dev, "power off cmd failed\n");

	mutex_unlock(&data->lock);

	return ret;
}

static int jsa1212_remove(struct i2c_client *client)
{
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
	struct jsa1212_data *data = iio_priv(indio_dev);

	iio_device_unregister(indio_dev);

	return jsa1212_power_off(data);
}

#ifdef CONFIG_PM_SLEEP
static int jsa1212_suspend(struct device *dev)
{
	struct jsa1212_data *data;

	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));

	return jsa1212_power_off(data);
}

static int jsa1212_resume(struct device *dev)
{
	int ret = 0;
	struct jsa1212_data *data;

	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));

	mutex_lock(&data->lock);

	if (data->als_en) {
		ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
		if (ret < 0) {
			dev_err(dev, "als resume failed\n");
			goto unlock_and_ret;
		}
	}

	if (data->pxs_en) {
		ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
		if (ret < 0)
			dev_err(dev, "pxs resume failed\n");
	}

unlock_and_ret:
	mutex_unlock(&data->lock);
	return ret;
}

static SIMPLE_DEV_PM_OPS(jsa1212_pm_ops, jsa1212_suspend, jsa1212_resume);

#define JSA1212_PM_OPS (&jsa1212_pm_ops)
#else
#define JSA1212_PM_OPS NULL
#endif

static const struct acpi_device_id jsa1212_acpi_match[] = {
	{"JSA1212", 0},
	{ },
};
MODULE_DEVICE_TABLE(acpi, jsa1212_acpi_match);

static const struct i2c_device_id jsa1212_id[] = {
	{ JSA1212_DRIVER_NAME, 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, jsa1212_id);

static struct i2c_driver jsa1212_driver = {
	.driver = {
		.name	= JSA1212_DRIVER_NAME,
		.pm	= JSA1212_PM_OPS,
		.acpi_match_table = ACPI_PTR(jsa1212_acpi_match),
	},
	.probe		= jsa1212_probe,
	.remove		= jsa1212_remove,
	.id_table	= jsa1212_id,
};
module_i2c_driver(jsa1212_driver);

MODULE_AUTHOR("Sathya Kuppuswamy <sathyanarayanan.kuppuswamy@linux.intel.com>");
MODULE_DESCRIPTION("JSA1212 proximity/ambient light sensor driver");
MODULE_LICENSE("GPL v2");