2 * ltr501.c - Support for Lite-On LTR501 ambient light and proximity sensor
4 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * 7-bit I2C slave address 0x23
12 * TODO: interrupt, threshold, measurement rate, IR LED characteristics
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/err.h>
18 #include <linux/delay.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/triggered_buffer.h>
26 #define LTR501_DRV_NAME "ltr501"
28 #define LTR501_ALS_CONTR 0x80 /* ALS operation mode, SW reset */
29 #define LTR501_PS_CONTR 0x81 /* PS operation mode */
30 #define LTR501_PART_ID 0x86
31 #define LTR501_MANUFAC_ID 0x87
32 #define LTR501_ALS_DATA1 0x88 /* 16-bit, little endian */
33 #define LTR501_ALS_DATA0 0x8a /* 16-bit, little endian */
34 #define LTR501_ALS_PS_STATUS 0x8c
35 #define LTR501_PS_DATA 0x8d /* 16-bit, little endian */
37 #define LTR501_ALS_CONTR_SW_RESET BIT(2)
38 #define LTR501_CONTR_PS_GAIN_MASK (BIT(3) | BIT(2))
39 #define LTR501_CONTR_PS_GAIN_SHIFT 2
40 #define LTR501_CONTR_ALS_GAIN_MASK BIT(3)
41 #define LTR501_CONTR_ACTIVE BIT(1)
43 #define LTR501_STATUS_ALS_RDY BIT(2)
44 #define LTR501_STATUS_PS_RDY BIT(0)
46 #define LTR501_PS_DATA_MASK 0x7ff
49 struct i2c_client
*client
;
50 struct mutex lock_als
, lock_ps
;
51 u8 als_contr
, ps_contr
;
54 static int ltr501_drdy(struct ltr501_data
*data
, u8 drdy_mask
)
60 ret
= i2c_smbus_read_byte_data(data
->client
,
61 LTR501_ALS_PS_STATUS
);
64 if ((ret
& drdy_mask
) == drdy_mask
)
69 dev_err(&data
->client
->dev
, "ltr501_drdy() failed, data not ready\n");
73 static int ltr501_read_als(struct ltr501_data
*data
, __le16 buf
[2])
75 int ret
= ltr501_drdy(data
, LTR501_STATUS_ALS_RDY
);
78 /* always read both ALS channels in given order */
79 return i2c_smbus_read_i2c_block_data(data
->client
,
80 LTR501_ALS_DATA1
, 2 * sizeof(__le16
), (u8
*) buf
);
83 static int ltr501_read_ps(struct ltr501_data
*data
)
85 int ret
= ltr501_drdy(data
, LTR501_STATUS_PS_RDY
);
88 return i2c_smbus_read_word_data(data
->client
, LTR501_PS_DATA
);
91 #define LTR501_INTENSITY_CHANNEL(_idx, _addr, _mod, _shared) { \
92 .type = IIO_INTENSITY, \
96 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
97 .info_mask_shared_by_type = (_shared), \
98 .scan_index = (_idx), \
103 .endianness = IIO_CPU, \
107 static const struct iio_chan_spec ltr501_channels
[] = {
108 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0
, IIO_MOD_LIGHT_BOTH
, 0),
109 LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1
, IIO_MOD_LIGHT_IR
,
110 BIT(IIO_CHAN_INFO_SCALE
)),
112 .type
= IIO_PROXIMITY
,
113 .address
= LTR501_PS_DATA
,
114 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
115 BIT(IIO_CHAN_INFO_SCALE
),
121 .endianness
= IIO_CPU
,
124 IIO_CHAN_SOFT_TIMESTAMP(3),
127 static const int ltr501_ps_gain
[4][2] = {
128 {1, 0}, {0, 250000}, {0, 125000}, {0, 62500}
131 static int ltr501_read_raw(struct iio_dev
*indio_dev
,
132 struct iio_chan_spec
const *chan
,
133 int *val
, int *val2
, long mask
)
135 struct ltr501_data
*data
= iio_priv(indio_dev
);
140 case IIO_CHAN_INFO_RAW
:
141 if (iio_buffer_enabled(indio_dev
))
144 switch (chan
->type
) {
146 mutex_lock(&data
->lock_als
);
147 ret
= ltr501_read_als(data
, buf
);
148 mutex_unlock(&data
->lock_als
);
151 *val
= le16_to_cpu(chan
->address
== LTR501_ALS_DATA1
?
155 mutex_lock(&data
->lock_ps
);
156 ret
= ltr501_read_ps(data
);
157 mutex_unlock(&data
->lock_ps
);
160 *val
= ret
& LTR501_PS_DATA_MASK
;
165 case IIO_CHAN_INFO_SCALE
:
166 switch (chan
->type
) {
168 if (data
->als_contr
& LTR501_CONTR_ALS_GAIN_MASK
) {
171 return IIO_VAL_INT_PLUS_MICRO
;
178 i
= (data
->ps_contr
& LTR501_CONTR_PS_GAIN_MASK
) >>
179 LTR501_CONTR_PS_GAIN_SHIFT
;
180 *val
= ltr501_ps_gain
[i
][0];
181 *val2
= ltr501_ps_gain
[i
][1];
182 return IIO_VAL_INT_PLUS_MICRO
;
190 static int ltr501_get_ps_gain_index(int val
, int val2
)
194 for (i
= 0; i
< ARRAY_SIZE(ltr501_ps_gain
); i
++)
195 if (val
== ltr501_ps_gain
[i
][0] && val2
== ltr501_ps_gain
[i
][1])
201 static int ltr501_write_raw(struct iio_dev
*indio_dev
,
202 struct iio_chan_spec
const *chan
,
203 int val
, int val2
, long mask
)
205 struct ltr501_data
*data
= iio_priv(indio_dev
);
208 if (iio_buffer_enabled(indio_dev
))
212 case IIO_CHAN_INFO_SCALE
:
213 switch (chan
->type
) {
215 if (val
== 0 && val2
== 5000)
216 data
->als_contr
|= LTR501_CONTR_ALS_GAIN_MASK
;
217 else if (val
== 1 && val2
== 0)
218 data
->als_contr
&= ~LTR501_CONTR_ALS_GAIN_MASK
;
221 return i2c_smbus_write_byte_data(data
->client
,
222 LTR501_ALS_CONTR
, data
->als_contr
);
224 i
= ltr501_get_ps_gain_index(val
, val2
);
227 data
->ps_contr
&= ~LTR501_CONTR_PS_GAIN_MASK
;
228 data
->ps_contr
|= i
<< LTR501_CONTR_PS_GAIN_SHIFT
;
229 return i2c_smbus_write_byte_data(data
->client
,
230 LTR501_PS_CONTR
, data
->ps_contr
);
238 static IIO_CONST_ATTR(in_proximity_scale_available
, "1 0.25 0.125 0.0625");
239 static IIO_CONST_ATTR(in_intensity_scale_available
, "1 0.005");
241 static struct attribute
*ltr501_attributes
[] = {
242 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
243 &iio_const_attr_in_intensity_scale_available
.dev_attr
.attr
,
247 static const struct attribute_group ltr501_attribute_group
= {
248 .attrs
= ltr501_attributes
,
251 static const struct iio_info ltr501_info
= {
252 .read_raw
= ltr501_read_raw
,
253 .write_raw
= ltr501_write_raw
,
254 .attrs
= <r501_attribute_group
,
255 .driver_module
= THIS_MODULE
,
258 static int ltr501_write_contr(struct i2c_client
*client
, u8 als_val
, u8 ps_val
)
260 int ret
= i2c_smbus_write_byte_data(client
, LTR501_ALS_CONTR
, als_val
);
264 return i2c_smbus_write_byte_data(client
, LTR501_PS_CONTR
, ps_val
);
267 static irqreturn_t
ltr501_trigger_handler(int irq
, void *p
)
269 struct iio_poll_func
*pf
= p
;
270 struct iio_dev
*indio_dev
= pf
->indio_dev
;
271 struct ltr501_data
*data
= iio_priv(indio_dev
);
278 memset(buf
, 0, sizeof(buf
));
280 /* figure out which data needs to be ready */
281 if (test_bit(0, indio_dev
->active_scan_mask
) ||
282 test_bit(1, indio_dev
->active_scan_mask
))
283 mask
|= LTR501_STATUS_ALS_RDY
;
284 if (test_bit(2, indio_dev
->active_scan_mask
))
285 mask
|= LTR501_STATUS_PS_RDY
;
287 ret
= ltr501_drdy(data
, mask
);
291 if (mask
& LTR501_STATUS_ALS_RDY
) {
292 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
293 LTR501_ALS_DATA1
, sizeof(als_buf
), (u8
*) als_buf
);
296 if (test_bit(0, indio_dev
->active_scan_mask
))
297 buf
[j
++] = le16_to_cpu(als_buf
[1]);
298 if (test_bit(1, indio_dev
->active_scan_mask
))
299 buf
[j
++] = le16_to_cpu(als_buf
[0]);
302 if (mask
& LTR501_STATUS_PS_RDY
) {
303 ret
= i2c_smbus_read_word_data(data
->client
, LTR501_PS_DATA
);
306 buf
[j
++] = ret
& LTR501_PS_DATA_MASK
;
309 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
313 iio_trigger_notify_done(indio_dev
->trig
);
318 static int ltr501_init(struct ltr501_data
*data
)
322 ret
= i2c_smbus_read_byte_data(data
->client
, LTR501_ALS_CONTR
);
325 data
->als_contr
= ret
| LTR501_CONTR_ACTIVE
;
327 ret
= i2c_smbus_read_byte_data(data
->client
, LTR501_PS_CONTR
);
330 data
->ps_contr
= ret
| LTR501_CONTR_ACTIVE
;
332 return ltr501_write_contr(data
->client
, data
->als_contr
,
336 static int ltr501_probe(struct i2c_client
*client
,
337 const struct i2c_device_id
*id
)
339 struct ltr501_data
*data
;
340 struct iio_dev
*indio_dev
;
343 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
347 data
= iio_priv(indio_dev
);
348 i2c_set_clientdata(client
, indio_dev
);
349 data
->client
= client
;
350 mutex_init(&data
->lock_als
);
351 mutex_init(&data
->lock_ps
);
353 ret
= i2c_smbus_read_byte_data(data
->client
, LTR501_PART_ID
);
356 if ((ret
>> 4) != 0x8)
359 indio_dev
->dev
.parent
= &client
->dev
;
360 indio_dev
->info
= <r501_info
;
361 indio_dev
->channels
= ltr501_channels
;
362 indio_dev
->num_channels
= ARRAY_SIZE(ltr501_channels
);
363 indio_dev
->name
= LTR501_DRV_NAME
;
364 indio_dev
->modes
= INDIO_DIRECT_MODE
;
366 ret
= ltr501_init(data
);
370 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
371 ltr501_trigger_handler
, NULL
);
375 ret
= iio_device_register(indio_dev
);
377 goto error_unreg_buffer
;
382 iio_triggered_buffer_cleanup(indio_dev
);
386 static int ltr501_powerdown(struct ltr501_data
*data
)
388 return ltr501_write_contr(data
->client
,
389 data
->als_contr
& ~LTR501_CONTR_ACTIVE
,
390 data
->ps_contr
& ~LTR501_CONTR_ACTIVE
);
393 static int ltr501_remove(struct i2c_client
*client
)
395 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
397 iio_device_unregister(indio_dev
);
398 iio_triggered_buffer_cleanup(indio_dev
);
399 ltr501_powerdown(iio_priv(indio_dev
));
404 #ifdef CONFIG_PM_SLEEP
405 static int ltr501_suspend(struct device
*dev
)
407 struct ltr501_data
*data
= iio_priv(i2c_get_clientdata(
408 to_i2c_client(dev
)));
409 return ltr501_powerdown(data
);
412 static int ltr501_resume(struct device
*dev
)
414 struct ltr501_data
*data
= iio_priv(i2c_get_clientdata(
415 to_i2c_client(dev
)));
417 return ltr501_write_contr(data
->client
, data
->als_contr
,
422 static SIMPLE_DEV_PM_OPS(ltr501_pm_ops
, ltr501_suspend
, ltr501_resume
);
424 static const struct i2c_device_id ltr501_id
[] = {
428 MODULE_DEVICE_TABLE(i2c
, ltr501_id
);
430 static struct i2c_driver ltr501_driver
= {
432 .name
= LTR501_DRV_NAME
,
433 .pm
= <r501_pm_ops
,
434 .owner
= THIS_MODULE
,
436 .probe
= ltr501_probe
,
437 .remove
= ltr501_remove
,
438 .id_table
= ltr501_id
,
441 module_i2c_driver(ltr501_driver
);
443 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
444 MODULE_DESCRIPTION("Lite-On LTR501 ambient light and proximity sensor driver");
445 MODULE_LICENSE("GPL");