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_preenable(struct iio_dev *indio_dev) 244 { 245 struct tcs3414_data *data = iio_priv(indio_dev); 246 247 data->control |= TCS3414_CONTROL_ADC_EN; 248 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 249 data->control); 250 } 251 252 static int tcs3414_buffer_predisable(struct iio_dev *indio_dev) 253 { 254 struct tcs3414_data *data = iio_priv(indio_dev); 255 int ret; 256 257 ret = iio_triggered_buffer_predisable(indio_dev); 258 if (ret < 0) 259 return ret; 260 261 data->control &= ~TCS3414_CONTROL_ADC_EN; 262 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 263 data->control); 264 } 265 266 static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = { 267 .preenable = tcs3414_buffer_preenable, 268 .postenable = &iio_triggered_buffer_postenable, 269 .predisable = tcs3414_buffer_predisable, 270 }; 271 272 static int tcs3414_probe(struct i2c_client *client, 273 const struct i2c_device_id *id) 274 { 275 struct tcs3414_data *data; 276 struct iio_dev *indio_dev; 277 int ret; 278 279 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 280 if (indio_dev == NULL) 281 return -ENOMEM; 282 283 data = iio_priv(indio_dev); 284 i2c_set_clientdata(client, indio_dev); 285 data->client = client; 286 287 indio_dev->dev.parent = &client->dev; 288 indio_dev->info = &tcs3414_info; 289 indio_dev->name = TCS3414_DRV_NAME; 290 indio_dev->channels = tcs3414_channels; 291 indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels); 292 indio_dev->modes = INDIO_DIRECT_MODE; 293 294 ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID); 295 if (ret < 0) 296 return ret; 297 298 switch (ret & 0xf0) { 299 case 0x00: 300 dev_info(&client->dev, "TCS3404 found\n"); 301 break; 302 case 0x10: 303 dev_info(&client->dev, "TCS3413/14/15/16 found\n"); 304 break; 305 default: 306 return -ENODEV; 307 } 308 309 data->control = TCS3414_CONTROL_POWER; 310 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 311 data->control); 312 if (ret < 0) 313 return ret; 314 315 data->timing = TCS3414_INTEG_12MS; /* free running */ 316 ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING, 317 data->timing); 318 if (ret < 0) 319 return ret; 320 321 ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN); 322 if (ret < 0) 323 return ret; 324 data->gain = ret; 325 326 ret = iio_triggered_buffer_setup(indio_dev, NULL, 327 tcs3414_trigger_handler, &tcs3414_buffer_setup_ops); 328 if (ret < 0) 329 return ret; 330 331 ret = iio_device_register(indio_dev); 332 if (ret < 0) 333 goto buffer_cleanup; 334 335 return 0; 336 337 buffer_cleanup: 338 iio_triggered_buffer_cleanup(indio_dev); 339 return ret; 340 } 341 342 static int tcs3414_powerdown(struct tcs3414_data *data) 343 { 344 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 345 data->control & ~(TCS3414_CONTROL_POWER | 346 TCS3414_CONTROL_ADC_EN)); 347 } 348 349 static int tcs3414_remove(struct i2c_client *client) 350 { 351 struct iio_dev *indio_dev = i2c_get_clientdata(client); 352 353 iio_device_unregister(indio_dev); 354 iio_triggered_buffer_cleanup(indio_dev); 355 tcs3414_powerdown(iio_priv(indio_dev)); 356 357 return 0; 358 } 359 360 #ifdef CONFIG_PM_SLEEP 361 static int tcs3414_suspend(struct device *dev) 362 { 363 struct tcs3414_data *data = iio_priv(i2c_get_clientdata( 364 to_i2c_client(dev))); 365 return tcs3414_powerdown(data); 366 } 367 368 static int tcs3414_resume(struct device *dev) 369 { 370 struct tcs3414_data *data = iio_priv(i2c_get_clientdata( 371 to_i2c_client(dev))); 372 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL, 373 data->control); 374 } 375 #endif 376 377 static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume); 378 379 static const struct i2c_device_id tcs3414_id[] = { 380 { "tcs3414", 0 }, 381 { } 382 }; 383 MODULE_DEVICE_TABLE(i2c, tcs3414_id); 384 385 static struct i2c_driver tcs3414_driver = { 386 .driver = { 387 .name = TCS3414_DRV_NAME, 388 .pm = &tcs3414_pm_ops, 389 }, 390 .probe = tcs3414_probe, 391 .remove = tcs3414_remove, 392 .id_table = tcs3414_id, 393 }; 394 module_i2c_driver(tcs3414_driver); 395 396 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 397 MODULE_DESCRIPTION("TCS3414 digital color sensors driver"); 398 MODULE_LICENSE("GPL"); 399