1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * tcs3414.c - Support for TAOS TCS3414 digital color sensor 4 * 5 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> 6 * 7 * Digital color sensor with 16-bit channels for red, green, blue, clear); 8 * 7-bit I2C slave address 0x39 (TCS3414) or 0x29, 0x49, 0x59 (TCS3413, 9 * TCS3415, TCS3416, resp.) 10 * 11 * TODO: sync, interrupt support, thresholds, prescaler 12 */ 13 14 #include <linux/module.h> 15 #include <linux/i2c.h> 16 #include <linux/delay.h> 17 #include <linux/pm.h> 18 19 #include <linux/iio/iio.h> 20 #include <linux/iio/sysfs.h> 21 #include <linux/iio/trigger_consumer.h> 22 #include <linux/iio/buffer.h> 23 #include <linux/iio/triggered_buffer.h> 24 25 #define TCS3414_DRV_NAME "tcs3414" 26 27 #define TCS3414_COMMAND BIT(7) 28 #define TCS3414_COMMAND_WORD (TCS3414_COMMAND | BIT(5)) 29 30 #define TCS3414_CONTROL (TCS3414_COMMAND | 0x00) 31 #define TCS3414_TIMING (TCS3414_COMMAND | 0x01) 32 #define TCS3414_ID (TCS3414_COMMAND | 0x04) 33 #define TCS3414_GAIN (TCS3414_COMMAND | 0x07) 34 #define TCS3414_DATA_GREEN (TCS3414_COMMAND_WORD | 0x10) 35 #define TCS3414_DATA_RED (TCS3414_COMMAND_WORD | 0x12) 36 #define TCS3414_DATA_BLUE (TCS3414_COMMAND_WORD | 0x14) 37 #define TCS3414_DATA_CLEAR (TCS3414_COMMAND_WORD | 0x16) 38 39 #define TCS3414_CONTROL_ADC_VALID BIT(4) 40 #define TCS3414_CONTROL_ADC_EN BIT(1) 41 #define TCS3414_CONTROL_POWER BIT(0) 42 43 #define TCS3414_INTEG_MASK GENMASK(1, 0) 44 #define TCS3414_INTEG_12MS 0x0 45 #define TCS3414_INTEG_100MS 0x1 46 #define TCS3414_INTEG_400MS 0x2 47 48 #define TCS3414_GAIN_MASK GENMASK(5, 4) 49 #define TCS3414_GAIN_SHIFT 4 50 51 struct tcs3414_data { 52 struct i2c_client *client; 53 u8 control; 54 u8 gain; 55 u8 timing; 56 u16 buffer[8]; /* 4x 16-bit + 8 bytes timestamp */ 57 }; 58 59 #define TCS3414_CHANNEL(_color, _si, _addr) { \ 60 .type = IIO_INTENSITY, \ 61 .modified = 1, \ 62 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 63 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 64 BIT(IIO_CHAN_INFO_INT_TIME), \ 65 .channel2 = IIO_MOD_LIGHT_##_color, \ 66 .address = _addr, \ 67 .scan_index = _si, \ 68 .scan_type = { \ 69 .sign = 'u', \ 70 .realbits = 16, \ 71 .storagebits = 16, \ 72 .endianness = IIO_CPU, \ 73 }, \ 74 } 75 76 /* scale factors: 1/gain */ 77 static const int tcs3414_scales[][2] = { 78 {1, 0}, {0, 250000}, {0, 62500}, {0, 15625} 79 }; 80 81 /* integration time in ms */ 82 static const int tcs3414_times[] = { 12, 100, 400 }; 83 84 static const struct iio_chan_spec tcs3414_channels[] = { 85 TCS3414_CHANNEL(GREEN, 0, TCS3414_DATA_GREEN), 86 TCS3414_CHANNEL(RED, 1, TCS3414_DATA_RED), 87 TCS3414_CHANNEL(BLUE, 2, TCS3414_DATA_BLUE), 88 TCS3414_CHANNEL(CLEAR, 3, TCS3414_DATA_CLEAR), 89 IIO_CHAN_SOFT_TIMESTAMP(4), 90 }; 91 92 static int tcs3414_req_data(struct tcs3414_data *data) 93 { 94 int tries = 25; 95 int ret; 96 97 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 98 data->control | TCS3414_CONTROL_ADC_EN); 99 if (ret < 0) 100 return ret; 101 102 while (tries--) { 103 ret = i2c_smbus_read_byte_data(data->client, TCS3414_CONTROL); 104 if (ret < 0) 105 return ret; 106 if (ret & TCS3414_CONTROL_ADC_VALID) 107 break; 108 msleep(20); 109 } 110 111 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 112 data->control); 113 if (ret < 0) 114 return ret; 115 116 if (tries < 0) { 117 dev_err(&data->client->dev, "data not ready\n"); 118 return -EIO; 119 } 120 121 return 0; 122 } 123 124 static int tcs3414_read_raw(struct iio_dev *indio_dev, 125 struct iio_chan_spec const *chan, 126 int *val, int *val2, long mask) 127 { 128 struct tcs3414_data *data = iio_priv(indio_dev); 129 int i, ret; 130 131 switch (mask) { 132 case IIO_CHAN_INFO_RAW: 133 ret = iio_device_claim_direct_mode(indio_dev); 134 if (ret) 135 return ret; 136 ret = tcs3414_req_data(data); 137 if (ret < 0) { 138 iio_device_release_direct_mode(indio_dev); 139 return ret; 140 } 141 ret = i2c_smbus_read_word_data(data->client, chan->address); 142 iio_device_release_direct_mode(indio_dev); 143 if (ret < 0) 144 return ret; 145 *val = ret; 146 return IIO_VAL_INT; 147 case IIO_CHAN_INFO_SCALE: 148 i = (data->gain & TCS3414_GAIN_MASK) >> TCS3414_GAIN_SHIFT; 149 *val = tcs3414_scales[i][0]; 150 *val2 = tcs3414_scales[i][1]; 151 return IIO_VAL_INT_PLUS_MICRO; 152 case IIO_CHAN_INFO_INT_TIME: 153 *val = 0; 154 *val2 = tcs3414_times[data->timing & TCS3414_INTEG_MASK] * 1000; 155 return IIO_VAL_INT_PLUS_MICRO; 156 } 157 return -EINVAL; 158 } 159 160 static int tcs3414_write_raw(struct iio_dev *indio_dev, 161 struct iio_chan_spec const *chan, 162 int val, int val2, long mask) 163 { 164 struct tcs3414_data *data = iio_priv(indio_dev); 165 int i; 166 167 switch (mask) { 168 case IIO_CHAN_INFO_SCALE: 169 for (i = 0; i < ARRAY_SIZE(tcs3414_scales); i++) { 170 if (val == tcs3414_scales[i][0] && 171 val2 == tcs3414_scales[i][1]) { 172 data->gain &= ~TCS3414_GAIN_MASK; 173 data->gain |= i << TCS3414_GAIN_SHIFT; 174 return i2c_smbus_write_byte_data( 175 data->client, TCS3414_GAIN, 176 data->gain); 177 } 178 } 179 return -EINVAL; 180 case IIO_CHAN_INFO_INT_TIME: 181 if (val != 0) 182 return -EINVAL; 183 for (i = 0; i < ARRAY_SIZE(tcs3414_times); i++) { 184 if (val2 == tcs3414_times[i] * 1000) { 185 data->timing &= ~TCS3414_INTEG_MASK; 186 data->timing |= i; 187 return i2c_smbus_write_byte_data( 188 data->client, TCS3414_TIMING, 189 data->timing); 190 } 191 } 192 return -EINVAL; 193 default: 194 return -EINVAL; 195 } 196 } 197 198 static irqreturn_t tcs3414_trigger_handler(int irq, void *p) 199 { 200 struct iio_poll_func *pf = p; 201 struct iio_dev *indio_dev = pf->indio_dev; 202 struct tcs3414_data *data = iio_priv(indio_dev); 203 int i, j = 0; 204 205 for_each_set_bit(i, indio_dev->active_scan_mask, 206 indio_dev->masklength) { 207 int ret = i2c_smbus_read_word_data(data->client, 208 TCS3414_DATA_GREEN + 2*i); 209 if (ret < 0) 210 goto done; 211 212 data->buffer[j++] = ret; 213 } 214 215 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 216 iio_get_time_ns(indio_dev)); 217 218 done: 219 iio_trigger_notify_done(indio_dev->trig); 220 221 return IRQ_HANDLED; 222 } 223 224 static IIO_CONST_ATTR(scale_available, "1 0.25 0.0625 0.015625"); 225 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.012 0.1 0.4"); 226 227 static struct attribute *tcs3414_attributes[] = { 228 &iio_const_attr_scale_available.dev_attr.attr, 229 &iio_const_attr_integration_time_available.dev_attr.attr, 230 NULL 231 }; 232 233 static const struct attribute_group tcs3414_attribute_group = { 234 .attrs = tcs3414_attributes, 235 }; 236 237 static const struct iio_info tcs3414_info = { 238 .read_raw = tcs3414_read_raw, 239 .write_raw = tcs3414_write_raw, 240 .attrs = &tcs3414_attribute_group, 241 }; 242 243 static int tcs3414_buffer_postenable(struct iio_dev *indio_dev) 244 { 245 struct tcs3414_data *data = iio_priv(indio_dev); 246 int ret; 247 248 ret = iio_triggered_buffer_postenable(indio_dev); 249 if (ret) 250 return ret; 251 252 data->control |= TCS3414_CONTROL_ADC_EN; 253 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 254 data->control); 255 if (ret) 256 iio_triggered_buffer_predisable(indio_dev); 257 258 return ret; 259 } 260 261 static int tcs3414_buffer_predisable(struct iio_dev *indio_dev) 262 { 263 struct tcs3414_data *data = iio_priv(indio_dev); 264 int ret, ret2; 265 266 data->control &= ~TCS3414_CONTROL_ADC_EN; 267 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 268 data->control); 269 270 ret2 = iio_triggered_buffer_predisable(indio_dev); 271 if (!ret) 272 ret = ret2; 273 274 return ret; 275 } 276 277 static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = { 278 .postenable = tcs3414_buffer_postenable, 279 .predisable = tcs3414_buffer_predisable, 280 }; 281 282 static int tcs3414_probe(struct i2c_client *client, 283 const struct i2c_device_id *id) 284 { 285 struct tcs3414_data *data; 286 struct iio_dev *indio_dev; 287 int ret; 288 289 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 290 if (indio_dev == NULL) 291 return -ENOMEM; 292 293 data = iio_priv(indio_dev); 294 i2c_set_clientdata(client, indio_dev); 295 data->client = client; 296 297 indio_dev->dev.parent = &client->dev; 298 indio_dev->info = &tcs3414_info; 299 indio_dev->name = TCS3414_DRV_NAME; 300 indio_dev->channels = tcs3414_channels; 301 indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels); 302 indio_dev->modes = INDIO_DIRECT_MODE; 303 304 ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID); 305 if (ret < 0) 306 return ret; 307 308 switch (ret & 0xf0) { 309 case 0x00: 310 dev_info(&client->dev, "TCS3404 found\n"); 311 break; 312 case 0x10: 313 dev_info(&client->dev, "TCS3413/14/15/16 found\n"); 314 break; 315 default: 316 return -ENODEV; 317 } 318 319 data->control = TCS3414_CONTROL_POWER; 320 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 321 data->control); 322 if (ret < 0) 323 return ret; 324 325 data->timing = TCS3414_INTEG_12MS; /* free running */ 326 ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING, 327 data->timing); 328 if (ret < 0) 329 return ret; 330 331 ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN); 332 if (ret < 0) 333 return ret; 334 data->gain = ret; 335 336 ret = iio_triggered_buffer_setup(indio_dev, NULL, 337 tcs3414_trigger_handler, &tcs3414_buffer_setup_ops); 338 if (ret < 0) 339 return ret; 340 341 ret = iio_device_register(indio_dev); 342 if (ret < 0) 343 goto buffer_cleanup; 344 345 return 0; 346 347 buffer_cleanup: 348 iio_triggered_buffer_cleanup(indio_dev); 349 return ret; 350 } 351 352 static int tcs3414_powerdown(struct tcs3414_data *data) 353 { 354 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 355 data->control & ~(TCS3414_CONTROL_POWER | 356 TCS3414_CONTROL_ADC_EN)); 357 } 358 359 static int tcs3414_remove(struct i2c_client *client) 360 { 361 struct iio_dev *indio_dev = i2c_get_clientdata(client); 362 363 iio_device_unregister(indio_dev); 364 iio_triggered_buffer_cleanup(indio_dev); 365 tcs3414_powerdown(iio_priv(indio_dev)); 366 367 return 0; 368 } 369 370 #ifdef CONFIG_PM_SLEEP 371 static int tcs3414_suspend(struct device *dev) 372 { 373 struct tcs3414_data *data = iio_priv(i2c_get_clientdata( 374 to_i2c_client(dev))); 375 return tcs3414_powerdown(data); 376 } 377 378 static int tcs3414_resume(struct device *dev) 379 { 380 struct tcs3414_data *data = iio_priv(i2c_get_clientdata( 381 to_i2c_client(dev))); 382 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 383 data->control); 384 } 385 #endif 386 387 static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume); 388 389 static const struct i2c_device_id tcs3414_id[] = { 390 { "tcs3414", 0 }, 391 { } 392 }; 393 MODULE_DEVICE_TABLE(i2c, tcs3414_id); 394 395 static struct i2c_driver tcs3414_driver = { 396 .driver = { 397 .name = TCS3414_DRV_NAME, 398 .pm = &tcs3414_pm_ops, 399 }, 400 .probe = tcs3414_probe, 401 .remove = tcs3414_remove, 402 .id_table = tcs3414_id, 403 }; 404 module_i2c_driver(tcs3414_driver); 405 406 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 407 MODULE_DESCRIPTION("TCS3414 digital color sensors driver"); 408 MODULE_LICENSE("GPL"); 409