perf bench futex: Cache align the worker struct
[linux/fpc-iii.git] / drivers / iio / light / jsa1212.c
blobe8a8931b4f50d0a2c1db1959a05b6722b2621d25
1 /*
2 * JSA1212 Ambient Light & Proximity Sensor Driver
4 * Copyright (c) 2014, Intel Corporation.
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.
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.
15 * JSA1212 I2C slave address: 0x44(ADDR tied to GND), 0x45(ADDR tied to VDD)
17 * TODO: Interrupt support, thresholds, range support.
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>
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
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
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
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
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
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
113 #define JSA1212_ALS_DELAY_MS 200
114 #define JSA1212_PXS_DELAY_MS 100
116 #define JSA1212_DRIVER_NAME "jsa1212"
117 #define JSA1212_REGMAP_NAME "jsa1212_regmap"
119 enum jsa1212_op_mode {
120 JSA1212_OPMODE_ALS_EN,
121 JSA1212_OPMODE_PXS_EN,
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;
133 /* ALS range idx to val mapping */
134 static const int jsa1212_als_range_val[] = {2048, 1024, 512, 256, 128,
135 128, 128, 128};
137 /* Enables or disables ALS function based on status */
138 static int jsa1212_als_enable(struct jsa1212_data *data, u8 status)
140 int ret;
142 ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
143 JSA1212_CONF_ALS_MASK,
144 status);
145 if (ret < 0)
146 return ret;
148 data->als_en = !!status;
150 return 0;
153 /* Enables or disables PXS function based on status */
154 static int jsa1212_pxs_enable(struct jsa1212_data *data, u8 status)
156 int ret;
158 ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
159 JSA1212_CONF_PXS_MASK,
160 status);
161 if (ret < 0)
162 return ret;
164 data->pxs_en = !!status;
166 return 0;
169 static int jsa1212_read_als_data(struct jsa1212_data *data,
170 unsigned int *val)
172 int ret;
173 __le16 als_data;
175 ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
176 if (ret < 0)
177 return ret;
179 /* Delay for data output */
180 msleep(JSA1212_ALS_DELAY_MS);
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;
189 *val = le16_to_cpu(als_data);
191 als_data_read_err:
192 return jsa1212_als_enable(data, JSA1212_CONF_ALS_DISABLE);
195 static int jsa1212_read_pxs_data(struct jsa1212_data *data,
196 unsigned int *val)
198 int ret;
199 unsigned int pxs_data;
201 ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
202 if (ret < 0)
203 return ret;
205 /* Delay for data output */
206 msleep(JSA1212_PXS_DELAY_MS);
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;
215 *val = pxs_data & JSA1212_PXS_DATA_MASK;
217 pxs_data_read_err:
218 return jsa1212_pxs_enable(data, JSA1212_CONF_PXS_DISABLE);
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)
225 int ret;
226 struct jsa1212_data *data = iio_priv(indio_dev);
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;
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;
253 break;
254 default:
255 break;
258 return -EINVAL;
261 static const struct iio_chan_spec jsa1212_channels[] = {
263 .type = IIO_LIGHT,
264 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
265 BIT(IIO_CHAN_INFO_SCALE),
268 .type = IIO_PROXIMITY,
269 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
273 static const struct iio_info jsa1212_info = {
274 .driver_module = THIS_MODULE,
275 .read_raw = &jsa1212_read_raw,
278 static int jsa1212_chip_init(struct jsa1212_data *data)
280 int ret;
282 ret = regmap_write(data->regmap, JSA1212_CONF_REG,
283 (JSA1212_CONF_PXS_SLP_50MS |
284 JSA1212_CONF_IRDR_200MA));
285 if (ret < 0)
286 return ret;
288 ret = regmap_write(data->regmap, JSA1212_INT_REG,
289 JSA1212_INT_ALS_PRST_4CONV);
290 if (ret < 0)
291 return ret;
293 data->als_rng_idx = JSA1212_ALS_RNG_0_2048;
295 return 0;
298 static bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
300 switch (reg) {
301 case JSA1212_PXS_DATA_REG:
302 case JSA1212_ALS_DT1_REG:
303 case JSA1212_ALS_DT2_REG:
304 case JSA1212_INT_REG:
305 return true;
306 default:
307 return false;
311 static const struct regmap_config jsa1212_regmap_config = {
312 .name = JSA1212_REGMAP_NAME,
313 .reg_bits = 8,
314 .val_bits = 8,
315 .max_register = JSA1212_MAX_REG,
316 .cache_type = REGCACHE_RBTREE,
317 .volatile_reg = jsa1212_is_volatile_reg,
320 static int jsa1212_probe(struct i2c_client *client,
321 const struct i2c_device_id *id)
323 struct jsa1212_data *data;
324 struct iio_dev *indio_dev;
325 struct regmap *regmap;
326 int ret;
328 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
329 if (!indio_dev)
330 return -ENOMEM;
332 regmap = devm_regmap_init_i2c(client, &jsa1212_regmap_config);
333 if (IS_ERR(regmap)) {
334 dev_err(&client->dev, "Regmap initialization failed.\n");
335 return PTR_ERR(regmap);
338 data = iio_priv(indio_dev);
340 i2c_set_clientdata(client, indio_dev);
341 data->client = client;
342 data->regmap = regmap;
344 mutex_init(&data->lock);
346 ret = jsa1212_chip_init(data);
347 if (ret < 0)
348 return ret;
350 indio_dev->dev.parent = &client->dev;
351 indio_dev->channels = jsa1212_channels;
352 indio_dev->num_channels = ARRAY_SIZE(jsa1212_channels);
353 indio_dev->name = JSA1212_DRIVER_NAME;
354 indio_dev->modes = INDIO_DIRECT_MODE;
356 indio_dev->info = &jsa1212_info;
358 ret = iio_device_register(indio_dev);
359 if (ret < 0)
360 dev_err(&client->dev, "%s: register device failed\n", __func__);
362 return ret;
365 /* power off the device */
366 static int jsa1212_power_off(struct jsa1212_data *data)
368 int ret;
370 mutex_lock(&data->lock);
372 ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
373 JSA1212_CONF_ALS_MASK |
374 JSA1212_CONF_PXS_MASK,
375 JSA1212_CONF_ALS_DISABLE |
376 JSA1212_CONF_PXS_DISABLE);
378 if (ret < 0)
379 dev_err(&data->client->dev, "power off cmd failed\n");
381 mutex_unlock(&data->lock);
383 return ret;
386 static int jsa1212_remove(struct i2c_client *client)
388 struct iio_dev *indio_dev = i2c_get_clientdata(client);
389 struct jsa1212_data *data = iio_priv(indio_dev);
391 iio_device_unregister(indio_dev);
393 return jsa1212_power_off(data);
396 #ifdef CONFIG_PM_SLEEP
397 static int jsa1212_suspend(struct device *dev)
399 struct jsa1212_data *data;
401 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
403 return jsa1212_power_off(data);
406 static int jsa1212_resume(struct device *dev)
408 int ret = 0;
409 struct jsa1212_data *data;
411 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
413 mutex_lock(&data->lock);
415 if (data->als_en) {
416 ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
417 if (ret < 0) {
418 dev_err(dev, "als resume failed\n");
419 goto unlock_and_ret;
423 if (data->pxs_en) {
424 ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
425 if (ret < 0)
426 dev_err(dev, "pxs resume failed\n");
429 unlock_and_ret:
430 mutex_unlock(&data->lock);
431 return ret;
434 static SIMPLE_DEV_PM_OPS(jsa1212_pm_ops, jsa1212_suspend, jsa1212_resume);
436 #define JSA1212_PM_OPS (&jsa1212_pm_ops)
437 #else
438 #define JSA1212_PM_OPS NULL
439 #endif
441 static const struct acpi_device_id jsa1212_acpi_match[] = {
442 {"JSA1212", 0},
443 { },
445 MODULE_DEVICE_TABLE(acpi, jsa1212_acpi_match);
447 static const struct i2c_device_id jsa1212_id[] = {
448 { JSA1212_DRIVER_NAME, 0 },
451 MODULE_DEVICE_TABLE(i2c, jsa1212_id);
453 static struct i2c_driver jsa1212_driver = {
454 .driver = {
455 .name = JSA1212_DRIVER_NAME,
456 .pm = JSA1212_PM_OPS,
457 .acpi_match_table = ACPI_PTR(jsa1212_acpi_match),
459 .probe = jsa1212_probe,
460 .remove = jsa1212_remove,
461 .id_table = jsa1212_id,
463 module_i2c_driver(jsa1212_driver);
465 MODULE_AUTHOR("Sathya Kuppuswamy <sathyanarayanan.kuppuswamy@linux.intel.com>");
466 MODULE_DESCRIPTION("JSA1212 proximity/ambient light sensor driver");
467 MODULE_LICENSE("GPL v2");