spi-tle62x0.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Support Infineon TLE62x0 driver chips
  3. *
  4. * Copyright (c) 2007 Simtec Electronics
  5. * Ben Dooks, <ben@simtec.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/spi/tle62x0.h>
  17. #define CMD_READ 0x00
  18. #define CMD_SET 0xff
  19. #define DIAG_NORMAL 0x03
  20. #define DIAG_OVERLOAD 0x02
  21. #define DIAG_OPEN 0x01
  22. #define DIAG_SHORTGND 0x00
  23. struct tle62x0_state {
  24. struct spi_device *us;
  25. struct mutex lock;
  26. unsigned int nr_gpio;
  27. unsigned int gpio_state;
  28. unsigned char tx_buff[4];
  29. unsigned char rx_buff[4];
  30. };
  31. static int to_gpio_num(struct device_attribute *attr);
  32. static inline int tle62x0_write(struct tle62x0_state *st)
  33. {
  34. unsigned char *buff = st->tx_buff;
  35. unsigned int gpio_state = st->gpio_state;
  36. buff[0] = CMD_SET;
  37. if (st->nr_gpio == 16) {
  38. buff[1] = gpio_state >> 8;
  39. buff[2] = gpio_state;
  40. } else {
  41. buff[1] = gpio_state;
  42. }
  43. dev_dbg(&st->us->dev, "buff %02x,%02x,%02x\n",
  44. buff[0], buff[1], buff[2]);
  45. return spi_write(st->us, buff, (st->nr_gpio == 16) ? 3 : 2);
  46. }
  47. static inline int tle62x0_read(struct tle62x0_state *st)
  48. {
  49. unsigned char *txbuff = st->tx_buff;
  50. struct spi_transfer xfer = {
  51. .tx_buf = txbuff,
  52. .rx_buf = st->rx_buff,
  53. .len = (st->nr_gpio * 2) / 8,
  54. };
  55. struct spi_message msg;
  56. txbuff[0] = CMD_READ;
  57. txbuff[1] = 0x00;
  58. txbuff[2] = 0x00;
  59. txbuff[3] = 0x00;
  60. spi_message_init(&msg);
  61. spi_message_add_tail(&xfer, &msg);
  62. return spi_sync(st->us, &msg);
  63. }
  64. static unsigned char *decode_fault(unsigned int fault_code)
  65. {
  66. fault_code &= 3;
  67. switch (fault_code) {
  68. case DIAG_NORMAL:
  69. return "N";
  70. case DIAG_OVERLOAD:
  71. return "V";
  72. case DIAG_OPEN:
  73. return "O";
  74. case DIAG_SHORTGND:
  75. return "G";
  76. }
  77. return "?";
  78. }
  79. static ssize_t tle62x0_status_show(struct device *dev,
  80. struct device_attribute *attr, char *buf)
  81. {
  82. struct tle62x0_state *st = dev_get_drvdata(dev);
  83. char *bp = buf;
  84. unsigned char *buff = st->rx_buff;
  85. unsigned long fault = 0;
  86. int ptr;
  87. int ret;
  88. mutex_lock(&st->lock);
  89. ret = tle62x0_read(st);
  90. dev_dbg(dev, "tle62x0_read() returned %d\n", ret);
  91. if (ret < 0) {
  92. mutex_unlock(&st->lock);
  93. return ret;
  94. }
  95. for (ptr = 0; ptr < (st->nr_gpio * 2)/8; ptr += 1) {
  96. fault <<= 8;
  97. fault |= ((unsigned long)buff[ptr]);
  98. dev_dbg(dev, "byte %d is %02x\n", ptr, buff[ptr]);
  99. }
  100. for (ptr = 0; ptr < st->nr_gpio; ptr++) {
  101. bp += sprintf(bp, "%s ", decode_fault(fault >> (ptr * 2)));
  102. }
  103. *bp++ = '\n';
  104. mutex_unlock(&st->lock);
  105. return bp - buf;
  106. }
  107. static DEVICE_ATTR(status_show, S_IRUGO, tle62x0_status_show, NULL);
  108. static ssize_t tle62x0_gpio_show(struct device *dev,
  109. struct device_attribute *attr, char *buf)
  110. {
  111. struct tle62x0_state *st = dev_get_drvdata(dev);
  112. int gpio_num = to_gpio_num(attr);
  113. int value;
  114. mutex_lock(&st->lock);
  115. value = (st->gpio_state >> gpio_num) & 1;
  116. mutex_unlock(&st->lock);
  117. return snprintf(buf, PAGE_SIZE, "%d", value);
  118. }
  119. static ssize_t tle62x0_gpio_store(struct device *dev,
  120. struct device_attribute *attr,
  121. const char *buf, size_t len)
  122. {
  123. struct tle62x0_state *st = dev_get_drvdata(dev);
  124. int gpio_num = to_gpio_num(attr);
  125. unsigned long val;
  126. char *endp;
  127. val = simple_strtoul(buf, &endp, 0);
  128. if (buf == endp)
  129. return -EINVAL;
  130. dev_dbg(dev, "setting gpio %d to %ld\n", gpio_num, val);
  131. mutex_lock(&st->lock);
  132. if (val)
  133. st->gpio_state |= 1 << gpio_num;
  134. else
  135. st->gpio_state &= ~(1 << gpio_num);
  136. tle62x0_write(st);
  137. mutex_unlock(&st->lock);
  138. return len;
  139. }
  140. static DEVICE_ATTR(gpio1, S_IWUSR|S_IRUGO,
  141. tle62x0_gpio_show, tle62x0_gpio_store);
  142. static DEVICE_ATTR(gpio2, S_IWUSR|S_IRUGO,
  143. tle62x0_gpio_show, tle62x0_gpio_store);
  144. static DEVICE_ATTR(gpio3, S_IWUSR|S_IRUGO,
  145. tle62x0_gpio_show, tle62x0_gpio_store);
  146. static DEVICE_ATTR(gpio4, S_IWUSR|S_IRUGO,
  147. tle62x0_gpio_show, tle62x0_gpio_store);
  148. static DEVICE_ATTR(gpio5, S_IWUSR|S_IRUGO,
  149. tle62x0_gpio_show, tle62x0_gpio_store);
  150. static DEVICE_ATTR(gpio6, S_IWUSR|S_IRUGO,
  151. tle62x0_gpio_show, tle62x0_gpio_store);
  152. static DEVICE_ATTR(gpio7, S_IWUSR|S_IRUGO,
  153. tle62x0_gpio_show, tle62x0_gpio_store);
  154. static DEVICE_ATTR(gpio8, S_IWUSR|S_IRUGO,
  155. tle62x0_gpio_show, tle62x0_gpio_store);
  156. static DEVICE_ATTR(gpio9, S_IWUSR|S_IRUGO,
  157. tle62x0_gpio_show, tle62x0_gpio_store);
  158. static DEVICE_ATTR(gpio10, S_IWUSR|S_IRUGO,
  159. tle62x0_gpio_show, tle62x0_gpio_store);
  160. static DEVICE_ATTR(gpio11, S_IWUSR|S_IRUGO,
  161. tle62x0_gpio_show, tle62x0_gpio_store);
  162. static DEVICE_ATTR(gpio12, S_IWUSR|S_IRUGO,
  163. tle62x0_gpio_show, tle62x0_gpio_store);
  164. static DEVICE_ATTR(gpio13, S_IWUSR|S_IRUGO,
  165. tle62x0_gpio_show, tle62x0_gpio_store);
  166. static DEVICE_ATTR(gpio14, S_IWUSR|S_IRUGO,
  167. tle62x0_gpio_show, tle62x0_gpio_store);
  168. static DEVICE_ATTR(gpio15, S_IWUSR|S_IRUGO,
  169. tle62x0_gpio_show, tle62x0_gpio_store);
  170. static DEVICE_ATTR(gpio16, S_IWUSR|S_IRUGO,
  171. tle62x0_gpio_show, tle62x0_gpio_store);
  172. static struct device_attribute *gpio_attrs[] = {
  173. [0] = &dev_attr_gpio1,
  174. [1] = &dev_attr_gpio2,
  175. [2] = &dev_attr_gpio3,
  176. [3] = &dev_attr_gpio4,
  177. [4] = &dev_attr_gpio5,
  178. [5] = &dev_attr_gpio6,
  179. [6] = &dev_attr_gpio7,
  180. [7] = &dev_attr_gpio8,
  181. [8] = &dev_attr_gpio9,
  182. [9] = &dev_attr_gpio10,
  183. [10] = &dev_attr_gpio11,
  184. [11] = &dev_attr_gpio12,
  185. [12] = &dev_attr_gpio13,
  186. [13] = &dev_attr_gpio14,
  187. [14] = &dev_attr_gpio15,
  188. [15] = &dev_attr_gpio16
  189. };
  190. static int to_gpio_num(struct device_attribute *attr)
  191. {
  192. int ptr;
  193. for (ptr = 0; ptr < ARRAY_SIZE(gpio_attrs); ptr++) {
  194. if (gpio_attrs[ptr] == attr)
  195. return ptr;
  196. }
  197. return -1;
  198. }
  199. static int __devinit tle62x0_probe(struct spi_device *spi)
  200. {
  201. struct tle62x0_state *st;
  202. struct tle62x0_pdata *pdata;
  203. int ptr;
  204. int ret;
  205. pdata = spi->dev.platform_data;
  206. if (pdata == NULL) {
  207. dev_err(&spi->dev, "no device data specified\n");
  208. return -EINVAL;
  209. }
  210. st = kzalloc(sizeof(struct tle62x0_state), GFP_KERNEL);
  211. if (st == NULL) {
  212. dev_err(&spi->dev, "no memory for device state\n");
  213. return -ENOMEM;
  214. }
  215. st->us = spi;
  216. st->nr_gpio = pdata->gpio_count;
  217. st->gpio_state = pdata->init_state;
  218. mutex_init(&st->lock);
  219. ret = device_create_file(&spi->dev, &dev_attr_status_show);
  220. if (ret) {
  221. dev_err(&spi->dev, "cannot create status attribute\n");
  222. goto err_status;
  223. }
  224. for (ptr = 0; ptr < pdata->gpio_count; ptr++) {
  225. ret = device_create_file(&spi->dev, gpio_attrs[ptr]);
  226. if (ret) {
  227. dev_err(&spi->dev, "cannot create gpio attribute\n");
  228. goto err_gpios;
  229. }
  230. }
  231. /* tle62x0_write(st); */
  232. spi_set_drvdata(spi, st);
  233. return 0;
  234. err_gpios:
  235. while (--ptr >= 0)
  236. device_remove_file(&spi->dev, gpio_attrs[ptr]);
  237. device_remove_file(&spi->dev, &dev_attr_status_show);
  238. err_status:
  239. kfree(st);
  240. return ret;
  241. }
  242. static int __devexit tle62x0_remove(struct spi_device *spi)
  243. {
  244. struct tle62x0_state *st = spi_get_drvdata(spi);
  245. int ptr;
  246. for (ptr = 0; ptr < st->nr_gpio; ptr++)
  247. device_remove_file(&spi->dev, gpio_attrs[ptr]);
  248. device_remove_file(&spi->dev, &dev_attr_status_show);
  249. kfree(st);
  250. return 0;
  251. }
  252. static struct spi_driver tle62x0_driver = {
  253. .driver = {
  254. .name = "tle62x0",
  255. .owner = THIS_MODULE,
  256. },
  257. .probe = tle62x0_probe,
  258. .remove = __devexit_p(tle62x0_remove),
  259. };
  260. static __init int tle62x0_init(void)
  261. {
  262. return spi_register_driver(&tle62x0_driver);
  263. }
  264. static __exit void tle62x0_exit(void)
  265. {
  266. spi_unregister_driver(&tle62x0_driver);
  267. }
  268. module_init(tle62x0_init);
  269. module_exit(tle62x0_exit);
  270. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  271. MODULE_DESCRIPTION("TLE62x0 SPI driver");
  272. MODULE_LICENSE("GPL v2");
  273. MODULE_ALIAS("spi:tle62x0");