1 /* 2 * AD7816 digital temperature sensor driver supporting AD7816/7/8 3 * 4 * Copyright 2010 Analog Devices Inc. 5 * 6 * Licensed under the GPL-2 or later. 7 */ 8 9 #include <linux/interrupt.h> 10 #include <linux/gpio.h> 11 #include <linux/device.h> 12 #include <linux/kernel.h> 13 #include <linux/slab.h> 14 #include <linux/sysfs.h> 15 #include <linux/list.h> 16 #include <linux/spi/spi.h> 17 #include <linux/module.h> 18 19 #include <linux/iio/iio.h> 20 #include <linux/iio/sysfs.h> 21 #include <linux/iio/events.h> 22 23 /* 24 * AD7816 config masks 25 */ 26 #define AD7816_FULL 0x1 27 #define AD7816_PD 0x2 28 #define AD7816_CS_MASK 0x7 29 #define AD7816_CS_MAX 0x4 30 31 /* 32 * AD7816 temperature masks 33 */ 34 #define AD7816_VALUE_OFFSET 6 35 #define AD7816_BOUND_VALUE_BASE 0x8 36 #define AD7816_BOUND_VALUE_MIN -95 37 #define AD7816_BOUND_VALUE_MAX 152 38 #define AD7816_TEMP_FLOAT_OFFSET 2 39 #define AD7816_TEMP_FLOAT_MASK 0x3 40 41 /* 42 * struct ad7816_chip_info - chip specific information 43 */ 44 45 struct ad7816_chip_info { 46 struct spi_device *spi_dev; 47 u16 rdwr_pin; 48 u16 convert_pin; 49 u16 busy_pin; 50 u8 oti_data[AD7816_CS_MAX + 1]; 51 u8 channel_id; /* 0 always be temperature */ 52 u8 mode; 53 }; 54 55 /* 56 * ad7816 data access by SPI 57 */ 58 static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data) 59 { 60 struct spi_device *spi_dev = chip->spi_dev; 61 int ret = 0; 62 __be16 buf; 63 64 gpio_set_value(chip->rdwr_pin, 1); 65 gpio_set_value(chip->rdwr_pin, 0); 66 ret = spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id)); 67 if (ret < 0) { 68 dev_err(&spi_dev->dev, "SPI channel setting error\n"); 69 return ret; 70 } 71 gpio_set_value(chip->rdwr_pin, 1); 72 73 if (chip->mode == AD7816_PD) { /* operating mode 2 */ 74 gpio_set_value(chip->convert_pin, 1); 75 gpio_set_value(chip->convert_pin, 0); 76 } else { /* operating mode 1 */ 77 gpio_set_value(chip->convert_pin, 0); 78 gpio_set_value(chip->convert_pin, 1); 79 } 80 81 while (gpio_get_value(chip->busy_pin)) 82 cpu_relax(); 83 84 gpio_set_value(chip->rdwr_pin, 0); 85 gpio_set_value(chip->rdwr_pin, 1); 86 ret = spi_read(spi_dev, &buf, sizeof(*data)); 87 if (ret < 0) { 88 dev_err(&spi_dev->dev, "SPI data read error\n"); 89 return ret; 90 } 91 92 *data = be16_to_cpu(buf); 93 94 return ret; 95 } 96 97 static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data) 98 { 99 struct spi_device *spi_dev = chip->spi_dev; 100 int ret = 0; 101 102 gpio_set_value(chip->rdwr_pin, 1); 103 gpio_set_value(chip->rdwr_pin, 0); 104 ret = spi_write(spi_dev, &data, sizeof(data)); 105 if (ret < 0) 106 dev_err(&spi_dev->dev, "SPI oti data write error\n"); 107 108 return ret; 109 } 110 111 static ssize_t ad7816_show_mode(struct device *dev, 112 struct device_attribute *attr, 113 char *buf) 114 { 115 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 116 struct ad7816_chip_info *chip = iio_priv(indio_dev); 117 118 if (chip->mode) 119 return sprintf(buf, "power-save\n"); 120 return sprintf(buf, "full\n"); 121 } 122 123 static ssize_t ad7816_store_mode(struct device *dev, 124 struct device_attribute *attr, 125 const char *buf, 126 size_t len) 127 { 128 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 129 struct ad7816_chip_info *chip = iio_priv(indio_dev); 130 131 if (strcmp(buf, "full")) { 132 gpio_set_value(chip->rdwr_pin, 1); 133 chip->mode = AD7816_FULL; 134 } else { 135 gpio_set_value(chip->rdwr_pin, 0); 136 chip->mode = AD7816_PD; 137 } 138 139 return len; 140 } 141 142 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, 143 ad7816_show_mode, 144 ad7816_store_mode, 145 0); 146 147 static ssize_t ad7816_show_available_modes(struct device *dev, 148 struct device_attribute *attr, 149 char *buf) 150 { 151 return sprintf(buf, "full\npower-save\n"); 152 } 153 154 static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7816_show_available_modes, 155 NULL, 0); 156 157 static ssize_t ad7816_show_channel(struct device *dev, 158 struct device_attribute *attr, 159 char *buf) 160 { 161 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 162 struct ad7816_chip_info *chip = iio_priv(indio_dev); 163 164 return sprintf(buf, "%d\n", chip->channel_id); 165 } 166 167 static ssize_t ad7816_store_channel(struct device *dev, 168 struct device_attribute *attr, 169 const char *buf, 170 size_t len) 171 { 172 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 173 struct ad7816_chip_info *chip = iio_priv(indio_dev); 174 unsigned long data; 175 int ret; 176 177 ret = kstrtoul(buf, 10, &data); 178 if (ret) 179 return ret; 180 181 if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) { 182 dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n", 183 data, indio_dev->name); 184 return -EINVAL; 185 } else if (strcmp(indio_dev->name, "ad7818") == 0 && data > 1) { 186 dev_err(&chip->spi_dev->dev, 187 "Invalid channel id %lu for ad7818.\n", data); 188 return -EINVAL; 189 } else if (strcmp(indio_dev->name, "ad7816") == 0 && data > 0) { 190 dev_err(&chip->spi_dev->dev, 191 "Invalid channel id %lu for ad7816.\n", data); 192 return -EINVAL; 193 } 194 195 chip->channel_id = data; 196 197 return len; 198 } 199 200 static IIO_DEVICE_ATTR(channel, S_IRUGO | S_IWUSR, 201 ad7816_show_channel, 202 ad7816_store_channel, 203 0); 204 205 static ssize_t ad7816_show_value(struct device *dev, 206 struct device_attribute *attr, 207 char *buf) 208 { 209 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 210 struct ad7816_chip_info *chip = iio_priv(indio_dev); 211 u16 data; 212 s8 value; 213 int ret; 214 215 ret = ad7816_spi_read(chip, &data); 216 if (ret) 217 return -EIO; 218 219 data >>= AD7816_VALUE_OFFSET; 220 221 if (chip->channel_id == 0) { 222 value = (s8)((data >> AD7816_TEMP_FLOAT_OFFSET) - 103); 223 data &= AD7816_TEMP_FLOAT_MASK; 224 if (value < 0) 225 data = (1 << AD7816_TEMP_FLOAT_OFFSET) - data; 226 return sprintf(buf, "%d.%.2d\n", value, data * 25); 227 } 228 return sprintf(buf, "%u\n", data); 229 } 230 231 static IIO_DEVICE_ATTR(value, S_IRUGO, ad7816_show_value, NULL, 0); 232 233 static struct attribute *ad7816_attributes[] = { 234 &iio_dev_attr_available_modes.dev_attr.attr, 235 &iio_dev_attr_mode.dev_attr.attr, 236 &iio_dev_attr_channel.dev_attr.attr, 237 &iio_dev_attr_value.dev_attr.attr, 238 NULL, 239 }; 240 241 static const struct attribute_group ad7816_attribute_group = { 242 .attrs = ad7816_attributes, 243 }; 244 245 /* 246 * temperature bound events 247 */ 248 249 #define IIO_EVENT_CODE_AD7816_OTI IIO_UNMOD_EVENT_CODE(IIO_TEMP, \ 250 0, \ 251 IIO_EV_TYPE_THRESH, \ 252 IIO_EV_DIR_FALLING) 253 254 static irqreturn_t ad7816_event_handler(int irq, void *private) 255 { 256 iio_push_event(private, IIO_EVENT_CODE_AD7816_OTI, 257 iio_get_time_ns((struct iio_dev *)private)); 258 return IRQ_HANDLED; 259 } 260 261 static ssize_t ad7816_show_oti(struct device *dev, 262 struct device_attribute *attr, 263 char *buf) 264 { 265 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 266 struct ad7816_chip_info *chip = iio_priv(indio_dev); 267 int value; 268 269 if (chip->channel_id > AD7816_CS_MAX) { 270 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id); 271 return -EINVAL; 272 } else if (chip->channel_id == 0) { 273 value = AD7816_BOUND_VALUE_MIN + 274 (chip->oti_data[chip->channel_id] - 275 AD7816_BOUND_VALUE_BASE); 276 return sprintf(buf, "%d\n", value); 277 } 278 return sprintf(buf, "%u\n", chip->oti_data[chip->channel_id]); 279 } 280 281 static inline ssize_t ad7816_set_oti(struct device *dev, 282 struct device_attribute *attr, 283 const char *buf, 284 size_t len) 285 { 286 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 287 struct ad7816_chip_info *chip = iio_priv(indio_dev); 288 long value; 289 u8 data; 290 int ret; 291 292 ret = kstrtol(buf, 10, &value); 293 if (ret) 294 return ret; 295 296 if (chip->channel_id > AD7816_CS_MAX) { 297 dev_err(dev, "Invalid oti channel id %d.\n", chip->channel_id); 298 return -EINVAL; 299 } else if (chip->channel_id == 0) { 300 if (value < AD7816_BOUND_VALUE_MIN || 301 value > AD7816_BOUND_VALUE_MAX) 302 return -EINVAL; 303 304 data = (u8)(value - AD7816_BOUND_VALUE_MIN + 305 AD7816_BOUND_VALUE_BASE); 306 } else { 307 if (value < AD7816_BOUND_VALUE_BASE || value > 255) 308 return -EINVAL; 309 310 data = (u8)value; 311 } 312 313 ret = ad7816_spi_write(chip, data); 314 if (ret) 315 return -EIO; 316 317 chip->oti_data[chip->channel_id] = data; 318 319 return len; 320 } 321 322 static IIO_DEVICE_ATTR(oti, S_IRUGO | S_IWUSR, 323 ad7816_show_oti, ad7816_set_oti, 0); 324 325 static struct attribute *ad7816_event_attributes[] = { 326 &iio_dev_attr_oti.dev_attr.attr, 327 NULL, 328 }; 329 330 static struct attribute_group ad7816_event_attribute_group = { 331 .attrs = ad7816_event_attributes, 332 .name = "events", 333 }; 334 335 static const struct iio_info ad7816_info = { 336 .attrs = &ad7816_attribute_group, 337 .event_attrs = &ad7816_event_attribute_group, 338 .driver_module = THIS_MODULE, 339 }; 340 341 /* 342 * device probe and remove 343 */ 344 345 static int ad7816_probe(struct spi_device *spi_dev) 346 { 347 struct ad7816_chip_info *chip; 348 struct iio_dev *indio_dev; 349 unsigned short *pins = dev_get_platdata(&spi_dev->dev); 350 int ret = 0; 351 int i; 352 353 if (!pins) { 354 dev_err(&spi_dev->dev, "No necessary GPIO platform data.\n"); 355 return -EINVAL; 356 } 357 358 indio_dev = devm_iio_device_alloc(&spi_dev->dev, sizeof(*chip)); 359 if (!indio_dev) 360 return -ENOMEM; 361 chip = iio_priv(indio_dev); 362 /* this is only used for device removal purposes */ 363 dev_set_drvdata(&spi_dev->dev, indio_dev); 364 365 chip->spi_dev = spi_dev; 366 for (i = 0; i <= AD7816_CS_MAX; i++) 367 chip->oti_data[i] = 203; 368 chip->rdwr_pin = pins[0]; 369 chip->convert_pin = pins[1]; 370 chip->busy_pin = pins[2]; 371 372 ret = devm_gpio_request(&spi_dev->dev, chip->rdwr_pin, 373 spi_get_device_id(spi_dev)->name); 374 if (ret) { 375 dev_err(&spi_dev->dev, "Fail to request rdwr gpio PIN %d.\n", 376 chip->rdwr_pin); 377 return ret; 378 } 379 gpio_direction_input(chip->rdwr_pin); 380 ret = devm_gpio_request(&spi_dev->dev, chip->convert_pin, 381 spi_get_device_id(spi_dev)->name); 382 if (ret) { 383 dev_err(&spi_dev->dev, "Fail to request convert gpio PIN %d.\n", 384 chip->convert_pin); 385 return ret; 386 } 387 gpio_direction_input(chip->convert_pin); 388 ret = devm_gpio_request(&spi_dev->dev, chip->busy_pin, 389 spi_get_device_id(spi_dev)->name); 390 if (ret) { 391 dev_err(&spi_dev->dev, "Fail to request busy gpio PIN %d.\n", 392 chip->busy_pin); 393 return ret; 394 } 395 gpio_direction_input(chip->busy_pin); 396 397 indio_dev->name = spi_get_device_id(spi_dev)->name; 398 indio_dev->dev.parent = &spi_dev->dev; 399 indio_dev->info = &ad7816_info; 400 indio_dev->modes = INDIO_DIRECT_MODE; 401 402 if (spi_dev->irq) { 403 /* Only low trigger is supported in ad7816/7/8 */ 404 ret = devm_request_threaded_irq(&spi_dev->dev, spi_dev->irq, 405 NULL, 406 &ad7816_event_handler, 407 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 408 indio_dev->name, 409 indio_dev); 410 if (ret) 411 return ret; 412 } 413 414 ret = devm_iio_device_register(&spi_dev->dev, indio_dev); 415 if (ret) 416 return ret; 417 418 dev_info(&spi_dev->dev, "%s temperature sensor and ADC registered.\n", 419 indio_dev->name); 420 421 return 0; 422 } 423 424 static const struct spi_device_id ad7816_id[] = { 425 { "ad7816", 0 }, 426 { "ad7817", 0 }, 427 { "ad7818", 0 }, 428 {} 429 }; 430 431 MODULE_DEVICE_TABLE(spi, ad7816_id); 432 433 static struct spi_driver ad7816_driver = { 434 .driver = { 435 .name = "ad7816", 436 }, 437 .probe = ad7816_probe, 438 .id_table = ad7816_id, 439 }; 440 module_spi_driver(ad7816_driver); 441 442 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); 443 MODULE_DESCRIPTION("Analog Devices AD7816/7/8 digital temperature sensor driver"); 444 MODULE_LICENSE("GPL v2"); 445