| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* isl29125.c - Support for Intersil ISL29125 RGB light sensor |
| 4 |
* |
| 5 |
* Copyright (c) 2014 Peter Meerwald <[email protected]> |
| 6 |
* |
| 7 |
* RGB light sensor with 16-bit channels for red, green, blue); |
| 8 |
* 7-bit I2C slave address 0x44 |
| 9 |
* |
| 10 |
* TODO: interrupt support, IR compensation, thresholds, 12bit |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/i2c.h> |
| 15 |
#include <linux/delay.h> |
| 16 |
#include <linux/pm.h> |
| 17 |
|
| 18 |
#include <linux/iio/iio.h> |
| 19 |
#include <linux/iio/sysfs.h> |
| 20 |
#include <linux/iio/trigger_consumer.h> |
| 21 |
#include <linux/iio/buffer.h> |
| 22 |
#include <linux/iio/triggered_buffer.h> |
| 23 |
|
| 24 |
#define ISL29125_DRV_NAME "isl29125" |
| 25 |
|
| 26 |
#define ISL29125_DEVICE_ID 0x00 |
| 27 |
#define ISL29125_CONF1 0x01 |
| 28 |
#define ISL29125_CONF2 0x02 |
| 29 |
#define ISL29125_CONF3 0x03 |
| 30 |
#define ISL29125_STATUS 0x08 |
| 31 |
#define ISL29125_GREEN_DATA 0x09 |
| 32 |
#define ISL29125_RED_DATA 0x0b |
| 33 |
#define ISL29125_BLUE_DATA 0x0d |
| 34 |
|
| 35 |
#define ISL29125_ID 0x7d |
| 36 |
|
| 37 |
#define ISL29125_MODE_MASK GENMASK(2, 0) |
| 38 |
#define ISL29125_MODE_PD 0x0 |
| 39 |
#define ISL29125_MODE_G 0x1 |
| 40 |
#define ISL29125_MODE_R 0x2 |
| 41 |
#define ISL29125_MODE_B 0x3 |
| 42 |
#define ISL29125_MODE_RGB 0x5 |
| 43 |
|
| 44 |
#define ISL29125_SENSING_RANGE_0 5722 /* 375 lux full range */ |
| 45 |
#define ISL29125_SENSING_RANGE_1 152590 /* 10k lux full range */ |
| 46 |
|
| 47 |
#define ISL29125_MODE_RANGE BIT(3) |
| 48 |
|
| 49 |
#define ISL29125_STATUS_CONV BIT(1) |
| 50 |
|
| 51 |
struct isl29125_data { |
| 52 |
struct i2c_client *client; |
| 53 |
u8 conf1; |
| 54 |
}; |
| 55 |
|
| 56 |
#define ISL29125_CHANNEL(_color, _si) { \ |
| 57 |
.type = IIO_INTENSITY, \ |
| 58 |
.modified = 1, \ |
| 59 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 60 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
| 61 |
.channel2 = IIO_MOD_LIGHT_##_color, \ |
| 62 |
.scan_index = _si, \ |
| 63 |
.scan_type = { \ |
| 64 |
.sign = 'u', \ |
| 65 |
.realbits = 16, \ |
| 66 |
.storagebits = 16, \ |
| 67 |
.endianness = IIO_CPU, \ |
| 68 |
}, \ |
| 69 |
} |
| 70 |
|
| 71 |
static const struct iio_chan_spec isl29125_channels[] = { |
| 72 |
ISL29125_CHANNEL(GREEN, 0), |
| 73 |
ISL29125_CHANNEL(RED, 1), |
| 74 |
ISL29125_CHANNEL(BLUE, 2), |
| 75 |
IIO_CHAN_SOFT_TIMESTAMP(3), |
| 76 |
}; |
| 77 |
|
| 78 |
static const struct { |
| 79 |
u8 mode, data; |
| 80 |
} isl29125_regs[] = { |
| 81 |
{ISL29125_MODE_G, ISL29125_GREEN_DATA}, |
| 82 |
{ISL29125_MODE_R, ISL29125_RED_DATA}, |
| 83 |
{ISL29125_MODE_B, ISL29125_BLUE_DATA}, |
| 84 |
}; |
| 85 |
|
| 86 |
static int isl29125_read_data(struct isl29125_data *data, int si) |
| 87 |
{ |
| 88 |
int tries = 5; |
| 89 |
int ret; |
| 90 |
|
| 91 |
ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, |
| 92 |
data->conf1 | isl29125_regs[si].mode); |
| 93 |
if (ret < 0) |
| 94 |
return ret; |
| 95 |
|
| 96 |
msleep(101); |
| 97 |
|
| 98 |
while (tries--) { |
| 99 |
ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS); |
| 100 |
if (ret < 0) |
| 101 |
goto fail; |
| 102 |
if (ret & ISL29125_STATUS_CONV) |
| 103 |
break; |
| 104 |
msleep(20); |
| 105 |
} |
| 106 |
|
| 107 |
if (tries < 0) { |
| 108 |
dev_err(&data->client->dev, "data not ready\n"); |
| 109 |
ret = -EIO; |
| 110 |
goto fail; |
| 111 |
} |
| 112 |
|
| 113 |
ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data); |
| 114 |
|
| 115 |
fail: |
| 116 |
i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1); |
| 117 |
return ret; |
| 118 |
} |
| 119 |
|
| 120 |
static int isl29125_read_raw(struct iio_dev *indio_dev, |
| 121 |
struct iio_chan_spec const *chan, |
| 122 |
int *val, int *val2, long mask) |
| 123 |
{ |
| 124 |
struct isl29125_data *data = iio_priv(indio_dev); |
| 125 |
int ret; |
| 126 |
|
| 127 |
switch (mask) { |
| 128 |
case IIO_CHAN_INFO_RAW: |
| 129 |
if (!iio_device_claim_direct(indio_dev)) |
| 130 |
return -EBUSY; |
| 131 |
ret = isl29125_read_data(data, chan->scan_index); |
| 132 |
iio_device_release_direct(indio_dev); |
| 133 |
if (ret < 0) |
| 134 |
return ret; |
| 135 |
*val = ret; |
| 136 |
return IIO_VAL_INT; |
| 137 |
case IIO_CHAN_INFO_SCALE: |
| 138 |
*val = 0; |
| 139 |
if (data->conf1 & ISL29125_MODE_RANGE) |
| 140 |
*val2 = ISL29125_SENSING_RANGE_1; /*10k lux full range*/ |
| 141 |
else |
| 142 |
*val2 = ISL29125_SENSING_RANGE_0; /*375 lux full range*/ |
| 143 |
return IIO_VAL_INT_PLUS_MICRO; |
| 144 |
} |
| 145 |
return -EINVAL; |
| 146 |
} |
| 147 |
|
| 148 |
static int isl29125_write_raw(struct iio_dev *indio_dev, |
| 149 |
struct iio_chan_spec const *chan, |
| 150 |
int val, int val2, long mask) |
| 151 |
{ |
| 152 |
struct isl29125_data *data = iio_priv(indio_dev); |
| 153 |
|
| 154 |
switch (mask) { |
| 155 |
case IIO_CHAN_INFO_SCALE: |
| 156 |
if (val != 0) |
| 157 |
return -EINVAL; |
| 158 |
if (val2 == ISL29125_SENSING_RANGE_1) |
| 159 |
data->conf1 |= ISL29125_MODE_RANGE; |
| 160 |
else if (val2 == ISL29125_SENSING_RANGE_0) |
| 161 |
data->conf1 &= ~ISL29125_MODE_RANGE; |
| 162 |
else |
| 163 |
return -EINVAL; |
| 164 |
return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, |
| 165 |
data->conf1); |
| 166 |
default: |
| 167 |
return -EINVAL; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static irqreturn_t isl29125_trigger_handler(int irq, void *p) |
| 172 |
{ |
| 173 |
struct iio_poll_func *pf = p; |
| 174 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 175 |
struct isl29125_data *data = iio_priv(indio_dev); |
| 176 |
int i, j = 0; |
| 177 |
/* Ensure timestamp is naturally aligned */ |
| 178 |
struct { |
| 179 |
u16 chans[3]; |
| 180 |
aligned_s64 timestamp; |
| 181 |
} scan = { }; |
| 182 |
|
| 183 |
iio_for_each_active_channel(indio_dev, i) { |
| 184 |
int ret = i2c_smbus_read_word_data(data->client, |
| 185 |
isl29125_regs[i].data); |
| 186 |
if (ret < 0) |
| 187 |
goto done; |
| 188 |
|
| 189 |
scan.chans[j++] = ret; |
| 190 |
} |
| 191 |
|
| 192 |
iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), |
| 193 |
iio_get_time_ns(indio_dev)); |
| 194 |
|
| 195 |
done: |
| 196 |
iio_trigger_notify_done(indio_dev->trig); |
| 197 |
|
| 198 |
return IRQ_HANDLED; |
| 199 |
} |
| 200 |
|
| 201 |
static IIO_CONST_ATTR(scale_available, "0.005722 0.152590"); |
| 202 |
|
| 203 |
static struct attribute *isl29125_attributes[] = { |
| 204 |
&iio_const_attr_scale_available.dev_attr.attr, |
| 205 |
NULL |
| 206 |
}; |
| 207 |
|
| 208 |
static const struct attribute_group isl29125_attribute_group = { |
| 209 |
.attrs = isl29125_attributes, |
| 210 |
}; |
| 211 |
|
| 212 |
static const struct iio_info isl29125_info = { |
| 213 |
.read_raw = isl29125_read_raw, |
| 214 |
.write_raw = isl29125_write_raw, |
| 215 |
.attrs = &isl29125_attribute_group, |
| 216 |
}; |
| 217 |
|
| 218 |
static int isl29125_buffer_postenable(struct iio_dev *indio_dev) |
| 219 |
{ |
| 220 |
struct isl29125_data *data = iio_priv(indio_dev); |
| 221 |
|
| 222 |
data->conf1 |= ISL29125_MODE_RGB; |
| 223 |
return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, |
| 224 |
data->conf1); |
| 225 |
} |
| 226 |
|
| 227 |
static int isl29125_buffer_predisable(struct iio_dev *indio_dev) |
| 228 |
{ |
| 229 |
struct isl29125_data *data = iio_priv(indio_dev); |
| 230 |
|
| 231 |
data->conf1 &= ~ISL29125_MODE_MASK; |
| 232 |
data->conf1 |= ISL29125_MODE_PD; |
| 233 |
return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, |
| 234 |
data->conf1); |
| 235 |
} |
| 236 |
|
| 237 |
static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = { |
| 238 |
.postenable = isl29125_buffer_postenable, |
| 239 |
.predisable = isl29125_buffer_predisable, |
| 240 |
}; |
| 241 |
|
| 242 |
static int isl29125_probe(struct i2c_client *client) |
| 243 |
{ |
| 244 |
struct isl29125_data *data; |
| 245 |
struct iio_dev *indio_dev; |
| 246 |
int ret; |
| 247 |
|
| 248 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 249 |
if (indio_dev == NULL) |
| 250 |
return -ENOMEM; |
| 251 |
|
| 252 |
data = iio_priv(indio_dev); |
| 253 |
i2c_set_clientdata(client, indio_dev); |
| 254 |
data->client = client; |
| 255 |
|
| 256 |
indio_dev->info = &isl29125_info; |
| 257 |
indio_dev->name = ISL29125_DRV_NAME; |
| 258 |
indio_dev->channels = isl29125_channels; |
| 259 |
indio_dev->num_channels = ARRAY_SIZE(isl29125_channels); |
| 260 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 261 |
|
| 262 |
ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID); |
| 263 |
if (ret < 0) |
| 264 |
return ret; |
| 265 |
if (ret != ISL29125_ID) |
| 266 |
return -ENODEV; |
| 267 |
|
| 268 |
data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE; |
| 269 |
ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, |
| 270 |
data->conf1); |
| 271 |
if (ret < 0) |
| 272 |
return ret; |
| 273 |
|
| 274 |
ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0); |
| 275 |
if (ret < 0) |
| 276 |
return ret; |
| 277 |
|
| 278 |
ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 279 |
isl29125_trigger_handler, &isl29125_buffer_setup_ops); |
| 280 |
if (ret < 0) |
| 281 |
return ret; |
| 282 |
|
| 283 |
ret = iio_device_register(indio_dev); |
| 284 |
if (ret < 0) |
| 285 |
goto buffer_cleanup; |
| 286 |
|
| 287 |
return 0; |
| 288 |
|
| 289 |
buffer_cleanup: |
| 290 |
iio_triggered_buffer_cleanup(indio_dev); |
| 291 |
return ret; |
| 292 |
} |
| 293 |
|
| 294 |
static int isl29125_powerdown(struct isl29125_data *data) |
| 295 |
{ |
| 296 |
return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, |
| 297 |
(data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD); |
| 298 |
} |
| 299 |
|
| 300 |
static void isl29125_remove(struct i2c_client *client) |
| 301 |
{ |
| 302 |
struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 303 |
|
| 304 |
iio_device_unregister(indio_dev); |
| 305 |
iio_triggered_buffer_cleanup(indio_dev); |
| 306 |
isl29125_powerdown(iio_priv(indio_dev)); |
| 307 |
} |
| 308 |
|
| 309 |
static int isl29125_suspend(struct device *dev) |
| 310 |
{ |
| 311 |
struct isl29125_data *data = iio_priv(i2c_get_clientdata( |
| 312 |
to_i2c_client(dev))); |
| 313 |
return isl29125_powerdown(data); |
| 314 |
} |
| 315 |
|
| 316 |
static int isl29125_resume(struct device *dev) |
| 317 |
{ |
| 318 |
struct isl29125_data *data = iio_priv(i2c_get_clientdata( |
| 319 |
to_i2c_client(dev))); |
| 320 |
return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, |
| 321 |
data->conf1); |
| 322 |
} |
| 323 |
|
| 324 |
static DEFINE_SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend, |
| 325 |
isl29125_resume); |
| 326 |
|
| 327 |
static const struct i2c_device_id isl29125_id[] = { |
| 328 |
{ "isl29125" }, |
| 329 |
{ } |
| 330 |
}; |
| 331 |
MODULE_DEVICE_TABLE(i2c, isl29125_id); |
| 332 |
|
| 333 |
static struct i2c_driver isl29125_driver = { |
| 334 |
.driver = { |
| 335 |
.name = ISL29125_DRV_NAME, |
| 336 |
.pm = pm_sleep_ptr(&isl29125_pm_ops), |
| 337 |
}, |
| 338 |
.probe = isl29125_probe, |
| 339 |
.remove = isl29125_remove, |
| 340 |
.id_table = isl29125_id, |
| 341 |
}; |
| 342 |
module_i2c_driver(isl29125_driver); |
| 343 |
|
| 344 |
MODULE_AUTHOR("Peter Meerwald <[email protected]>"); |
| 345 |
MODULE_DESCRIPTION("ISL29125 RGB light sensor driver"); |
| 346 |
MODULE_LICENSE("GPL"); |