Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / light / pa12203001.c
blob30ea1a088dd907993ca74440bf769fdf0776777a
1 /*
2 * Copyright (c) 2015 Intel Corporation
4 * Driver for TXC PA12203001 Proximity and Ambient Light Sensor.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 * To do: Interrupt support.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/mutex.h>
20 #include <linux/pm.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
24 #define PA12203001_DRIVER_NAME "pa12203001"
26 #define PA12203001_REG_CFG0 0x00
27 #define PA12203001_REG_CFG1 0x01
28 #define PA12203001_REG_CFG2 0x02
29 #define PA12203001_REG_CFG3 0x03
31 #define PA12203001_REG_ADL 0x0b
32 #define PA12203001_REG_PDH 0x0e
34 #define PA12203001_REG_POFS 0x10
35 #define PA12203001_REG_PSET 0x11
37 #define PA12203001_ALS_EN_MASK BIT(0)
38 #define PA12203001_PX_EN_MASK BIT(1)
39 #define PA12203001_PX_NORMAL_MODE_MASK GENMASK(7, 6)
40 #define PA12203001_AFSR_MASK GENMASK(5, 4)
41 #define PA12203001_AFSR_SHIFT 4
43 #define PA12203001_PSCAN 0x03
45 /* als range 31000, ps, als disabled */
46 #define PA12203001_REG_CFG0_DEFAULT 0x30
48 /* led current: 100 mA */
49 #define PA12203001_REG_CFG1_DEFAULT 0x20
51 /* ps mode: normal, interrupts not active */
52 #define PA12203001_REG_CFG2_DEFAULT 0xcc
54 #define PA12203001_REG_CFG3_DEFAULT 0x00
56 #define PA12203001_SLEEP_DELAY_MS 3000
58 #define PA12203001_CHIP_ENABLE 0xff
59 #define PA12203001_CHIP_DISABLE 0x00
61 /* available scales: corresponding to [500, 4000, 7000, 31000] lux */
62 static const int pa12203001_scales[] = { 7629, 61036, 106813, 473029};
64 struct pa12203001_data {
65 struct i2c_client *client;
67 /* protect device states */
68 struct mutex lock;
70 bool als_enabled;
71 bool px_enabled;
72 bool als_needs_enable;
73 bool px_needs_enable;
75 struct regmap *map;
78 static const struct {
79 u8 reg;
80 u8 val;
81 } regvals[] = {
82 {PA12203001_REG_CFG0, PA12203001_REG_CFG0_DEFAULT},
83 {PA12203001_REG_CFG1, PA12203001_REG_CFG1_DEFAULT},
84 {PA12203001_REG_CFG2, PA12203001_REG_CFG2_DEFAULT},
85 {PA12203001_REG_CFG3, PA12203001_REG_CFG3_DEFAULT},
86 {PA12203001_REG_PSET, PA12203001_PSCAN},
89 static IIO_CONST_ATTR(in_illuminance_scale_available,
90 "0.007629 0.061036 0.106813 0.473029");
92 static struct attribute *pa12203001_attrs[] = {
93 &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
94 NULL
97 static const struct attribute_group pa12203001_attr_group = {
98 .attrs = pa12203001_attrs,
101 static const struct iio_chan_spec pa12203001_channels[] = {
103 .type = IIO_LIGHT,
104 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
105 BIT(IIO_CHAN_INFO_SCALE),
108 .type = IIO_PROXIMITY,
109 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
113 static const struct regmap_range pa12203001_volatile_regs_ranges[] = {
114 regmap_reg_range(PA12203001_REG_ADL, PA12203001_REG_ADL + 1),
115 regmap_reg_range(PA12203001_REG_PDH, PA12203001_REG_PDH),
118 static const struct regmap_access_table pa12203001_volatile_regs = {
119 .yes_ranges = pa12203001_volatile_regs_ranges,
120 .n_yes_ranges = ARRAY_SIZE(pa12203001_volatile_regs_ranges),
123 static const struct regmap_config pa12203001_regmap_config = {
124 .reg_bits = 8,
125 .val_bits = 8,
126 .max_register = PA12203001_REG_PSET,
127 .cache_type = REGCACHE_RBTREE,
128 .volatile_table = &pa12203001_volatile_regs,
131 static inline int pa12203001_als_enable(struct pa12203001_data *data, u8 enable)
133 int ret;
135 ret = regmap_update_bits(data->map, PA12203001_REG_CFG0,
136 PA12203001_ALS_EN_MASK, enable);
137 if (ret < 0)
138 return ret;
140 data->als_enabled = !!enable;
142 return 0;
145 static inline int pa12203001_px_enable(struct pa12203001_data *data, u8 enable)
147 int ret;
149 ret = regmap_update_bits(data->map, PA12203001_REG_CFG0,
150 PA12203001_PX_EN_MASK, enable);
151 if (ret < 0)
152 return ret;
154 data->px_enabled = !!enable;
156 return 0;
159 static int pa12203001_set_power_state(struct pa12203001_data *data, bool on,
160 u8 mask)
162 #ifdef CONFIG_PM
163 int ret;
165 if (on && (mask & PA12203001_ALS_EN_MASK)) {
166 mutex_lock(&data->lock);
167 if (data->px_enabled) {
168 ret = pa12203001_als_enable(data,
169 PA12203001_ALS_EN_MASK);
170 if (ret < 0)
171 goto err;
172 } else {
173 data->als_needs_enable = true;
175 mutex_unlock(&data->lock);
178 if (on && (mask & PA12203001_PX_EN_MASK)) {
179 mutex_lock(&data->lock);
180 if (data->als_enabled) {
181 ret = pa12203001_px_enable(data, PA12203001_PX_EN_MASK);
182 if (ret < 0)
183 goto err;
184 } else {
185 data->px_needs_enable = true;
187 mutex_unlock(&data->lock);
190 if (on) {
191 ret = pm_runtime_get_sync(&data->client->dev);
192 if (ret < 0)
193 pm_runtime_put_noidle(&data->client->dev);
195 } else {
196 pm_runtime_mark_last_busy(&data->client->dev);
197 ret = pm_runtime_put_autosuspend(&data->client->dev);
200 return ret;
202 err:
203 mutex_unlock(&data->lock);
204 return ret;
206 #endif
207 return 0;
210 static int pa12203001_read_raw(struct iio_dev *indio_dev,
211 struct iio_chan_spec const *chan, int *val,
212 int *val2, long mask)
214 struct pa12203001_data *data = iio_priv(indio_dev);
215 int ret;
216 u8 dev_mask;
217 unsigned int reg_byte;
218 __le16 reg_word;
220 switch (mask) {
221 case IIO_CHAN_INFO_RAW:
222 switch (chan->type) {
223 case IIO_LIGHT:
224 dev_mask = PA12203001_ALS_EN_MASK;
225 ret = pa12203001_set_power_state(data, true, dev_mask);
226 if (ret < 0)
227 return ret;
229 * ALS ADC value is stored in registers
230 * PA12203001_REG_ADL and in PA12203001_REG_ADL + 1.
232 ret = regmap_bulk_read(data->map, PA12203001_REG_ADL,
233 &reg_word, 2);
234 if (ret < 0)
235 goto reg_err;
237 *val = le16_to_cpu(reg_word);
238 ret = pa12203001_set_power_state(data, false, dev_mask);
239 if (ret < 0)
240 return ret;
241 break;
242 case IIO_PROXIMITY:
243 dev_mask = PA12203001_PX_EN_MASK;
244 ret = pa12203001_set_power_state(data, true, dev_mask);
245 if (ret < 0)
246 return ret;
247 ret = regmap_read(data->map, PA12203001_REG_PDH,
248 &reg_byte);
249 if (ret < 0)
250 goto reg_err;
252 *val = reg_byte;
253 ret = pa12203001_set_power_state(data, false, dev_mask);
254 if (ret < 0)
255 return ret;
256 break;
257 default:
258 return -EINVAL;
260 return IIO_VAL_INT;
261 case IIO_CHAN_INFO_SCALE:
262 ret = regmap_read(data->map, PA12203001_REG_CFG0, &reg_byte);
263 if (ret < 0)
264 return ret;
265 *val = 0;
266 reg_byte = (reg_byte & PA12203001_AFSR_MASK);
267 *val2 = pa12203001_scales[reg_byte >> 4];
268 return IIO_VAL_INT_PLUS_MICRO;
269 default:
270 return -EINVAL;
273 reg_err:
274 pa12203001_set_power_state(data, false, dev_mask);
275 return ret;
278 static int pa12203001_write_raw(struct iio_dev *indio_dev,
279 struct iio_chan_spec const *chan, int val,
280 int val2, long mask)
282 struct pa12203001_data *data = iio_priv(indio_dev);
283 int i, ret, new_val;
284 unsigned int reg_byte;
286 switch (mask) {
287 case IIO_CHAN_INFO_SCALE:
288 ret = regmap_read(data->map, PA12203001_REG_CFG0, &reg_byte);
289 if (val != 0 || ret < 0)
290 return -EINVAL;
291 for (i = 0; i < ARRAY_SIZE(pa12203001_scales); i++) {
292 if (val2 == pa12203001_scales[i]) {
293 new_val = i << PA12203001_AFSR_SHIFT;
294 return regmap_update_bits(data->map,
295 PA12203001_REG_CFG0,
296 PA12203001_AFSR_MASK,
297 new_val);
300 break;
301 default:
302 break;
305 return -EINVAL;
308 static const struct iio_info pa12203001_info = {
309 .read_raw = pa12203001_read_raw,
310 .write_raw = pa12203001_write_raw,
311 .attrs = &pa12203001_attr_group,
314 static int pa12203001_init(struct iio_dev *indio_dev)
316 struct pa12203001_data *data = iio_priv(indio_dev);
317 int i, ret;
319 for (i = 0; i < ARRAY_SIZE(regvals); i++) {
320 ret = regmap_write(data->map, regvals[i].reg, regvals[i].val);
321 if (ret < 0)
322 return ret;
325 return 0;
328 static int pa12203001_power_chip(struct iio_dev *indio_dev, u8 state)
330 struct pa12203001_data *data = iio_priv(indio_dev);
331 int ret;
333 mutex_lock(&data->lock);
334 ret = pa12203001_als_enable(data, state);
335 if (ret < 0)
336 goto out;
338 ret = pa12203001_px_enable(data, state);
340 out:
341 mutex_unlock(&data->lock);
342 return ret;
345 static int pa12203001_probe(struct i2c_client *client,
346 const struct i2c_device_id *id)
348 struct pa12203001_data *data;
349 struct iio_dev *indio_dev;
350 int ret;
352 indio_dev = devm_iio_device_alloc(&client->dev,
353 sizeof(struct pa12203001_data));
354 if (!indio_dev)
355 return -ENOMEM;
357 data = iio_priv(indio_dev);
358 i2c_set_clientdata(client, indio_dev);
359 data->client = client;
361 data->map = devm_regmap_init_i2c(client, &pa12203001_regmap_config);
362 if (IS_ERR(data->map))
363 return PTR_ERR(data->map);
365 mutex_init(&data->lock);
367 indio_dev->dev.parent = &client->dev;
368 indio_dev->info = &pa12203001_info;
369 indio_dev->name = PA12203001_DRIVER_NAME;
370 indio_dev->channels = pa12203001_channels;
371 indio_dev->num_channels = ARRAY_SIZE(pa12203001_channels);
372 indio_dev->modes = INDIO_DIRECT_MODE;
374 ret = pa12203001_init(indio_dev);
375 if (ret < 0)
376 return ret;
378 ret = pa12203001_power_chip(indio_dev, PA12203001_CHIP_ENABLE);
379 if (ret < 0)
380 return ret;
382 ret = pm_runtime_set_active(&client->dev);
383 if (ret < 0)
384 goto out_err;
386 pm_runtime_enable(&client->dev);
387 pm_runtime_set_autosuspend_delay(&client->dev,
388 PA12203001_SLEEP_DELAY_MS);
389 pm_runtime_use_autosuspend(&client->dev);
391 ret = iio_device_register(indio_dev);
392 if (ret < 0)
393 goto out_err;
395 return 0;
397 out_err:
398 pa12203001_power_chip(indio_dev, PA12203001_CHIP_DISABLE);
399 return ret;
402 static int pa12203001_remove(struct i2c_client *client)
404 struct iio_dev *indio_dev = i2c_get_clientdata(client);
406 iio_device_unregister(indio_dev);
408 pm_runtime_disable(&client->dev);
409 pm_runtime_set_suspended(&client->dev);
411 return pa12203001_power_chip(indio_dev, PA12203001_CHIP_DISABLE);
414 #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM)
415 static int pa12203001_suspend(struct device *dev)
417 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
419 return pa12203001_power_chip(indio_dev, PA12203001_CHIP_DISABLE);
421 #endif
423 #ifdef CONFIG_PM_SLEEP
424 static int pa12203001_resume(struct device *dev)
426 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
428 return pa12203001_power_chip(indio_dev, PA12203001_CHIP_ENABLE);
430 #endif
432 #ifdef CONFIG_PM
433 static int pa12203001_runtime_resume(struct device *dev)
435 struct pa12203001_data *data;
437 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
439 mutex_lock(&data->lock);
440 if (data->als_needs_enable) {
441 pa12203001_als_enable(data, PA12203001_ALS_EN_MASK);
442 data->als_needs_enable = false;
444 if (data->px_needs_enable) {
445 pa12203001_px_enable(data, PA12203001_PX_EN_MASK);
446 data->px_needs_enable = false;
448 mutex_unlock(&data->lock);
450 return 0;
452 #endif
454 static const struct dev_pm_ops pa12203001_pm_ops = {
455 SET_SYSTEM_SLEEP_PM_OPS(pa12203001_suspend, pa12203001_resume)
456 SET_RUNTIME_PM_OPS(pa12203001_suspend, pa12203001_runtime_resume, NULL)
459 static const struct acpi_device_id pa12203001_acpi_match[] = {
460 { "TXCPA122", 0},
464 MODULE_DEVICE_TABLE(acpi, pa12203001_acpi_match);
466 static const struct i2c_device_id pa12203001_id[] = {
467 {"txcpa122", 0},
471 MODULE_DEVICE_TABLE(i2c, pa12203001_id);
473 static struct i2c_driver pa12203001_driver = {
474 .driver = {
475 .name = PA12203001_DRIVER_NAME,
476 .pm = &pa12203001_pm_ops,
477 .acpi_match_table = ACPI_PTR(pa12203001_acpi_match),
479 .probe = pa12203001_probe,
480 .remove = pa12203001_remove,
481 .id_table = pa12203001_id,
484 module_i2c_driver(pa12203001_driver);
486 MODULE_AUTHOR("Adriana Reus <adriana.reus@intel.com>");
487 MODULE_DESCRIPTION("Driver for TXC PA12203001 Proximity and Light Sensor");
488 MODULE_LICENSE("GPL v2");