spi-tle62x0.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 %3ph\n", buff);
  44. return spi_write(st->us, buff, (st->nr_gpio == 16) ? 3 : 2);
  45. }
  46. static inline int tle62x0_read(struct tle62x0_state *st)
  47. {
  48. unsigned char *txbuff = st->tx_buff;
  49. struct spi_transfer xfer = {
  50. .tx_buf = txbuff,
  51. .rx_buf = st->rx_buff,
  52. .len = (st->nr_gpio * 2) / 8,
  53. };
  54. struct spi_message msg;
  55. txbuff[0] = CMD_READ;
  56. txbuff[1] = 0x00;
  57. txbuff[2] = 0x00;
  58. txbuff[3] = 0x00;
  59. spi_message_init(&msg);
  60. spi_message_add_tail(&xfer, &msg);
  61. return spi_sync(st->us, &msg);
  62. }
  63. static unsigned char *decode_fault(unsigned int fault_code)
  64. {
  65. fault_code &= 3;
  66. switch (fault_code) {
  67. case DIAG_NORMAL:
  68. return "N";
  69. case DIAG_OVERLOAD:
  70. return "V";
  71. case DIAG_OPEN:
  72. return "O";
  73. case DIAG_SHORTGND:
  74. return "G";
  75. }
  76. return "?";
  77. }
  78. static ssize_t tle62x0_status_show(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct tle62x0_state *st = dev_get_drvdata(dev);
  82. char *bp = buf;
  83. unsigned char *buff = st->rx_buff;
  84. unsigned long fault = 0;
  85. int ptr;
  86. int ret;
  87. mutex_lock(&st->lock);
  88. ret = tle62x0_read(st);
  89. dev_dbg(dev, "tle62x0_read() returned %d\n", ret);
  90. if (ret < 0) {
  91. mutex_unlock(&st->lock);
  92. return ret;
  93. }
  94. for (ptr = 0; ptr < (st->nr_gpio * 2)/8; ptr += 1) {
  95. fault <<= 8;
  96. fault |= ((unsigned long)buff[ptr]);
  97. dev_dbg(dev, "byte %d is %02x\n", ptr, buff[ptr]);
  98. }
  99. for (ptr = 0; ptr < st->nr_gpio; ptr++) {
  100. bp += sprintf(bp, "%s ", decode_fault(fault >> (ptr * 2)));
  101. }
  102. *bp++ = '\n';
  103. mutex_unlock(&st->lock);
  104. return bp - buf;
  105. }
  106. static DEVICE_ATTR(status_show, S_IRUGO, tle62x0_status_show, NULL);
  107. static ssize_t tle62x0_gpio_show(struct device *dev,
  108. struct device_attribute *attr, char *buf)
  109. {
  110. struct tle62x0_state *st = dev_get_drvdata(dev);
  111. int gpio_num = to_gpio_num(attr);
  112. int value;
  113. mutex_lock(&st->lock);
  114. value = (st->gpio_state >> gpio_num) & 1;
  115. mutex_unlock(&st->lock);
  116. return snprintf(buf, PAGE_SIZE, "%d", value);
  117. }
  118. static ssize_t tle62x0_gpio_store(struct device *dev,
  119. struct device_attribute *attr,
  120. const char *buf, size_t len)
  121. {
  122. struct tle62x0_state *st = dev_get_drvdata(dev);
  123. int gpio_num = to_gpio_num(attr);
  124. unsigned long val;
  125. char *endp;
  126. val = simple_strtoul(buf, &endp, 0);
  127. if (buf == endp)
  128. return -EINVAL;
  129. dev_dbg(dev, "setting gpio %d to %ld\n", gpio_num, val);
  130. mutex_lock(&st->lock);
  131. if (val)
  132. st->gpio_state |= 1 << gpio_num;
  133. else
  134. st->gpio_state &= ~(1 << gpio_num);
  135. tle62x0_write(st);
  136. mutex_unlock(&st->lock);
  137. return len;
  138. }
  139. static DEVICE_ATTR(gpio1, S_IWUSR|S_IRUGO,
  140. tle62x0_gpio_show, tle62x0_gpio_store);
  141. static DEVICE_ATTR(gpio2, S_IWUSR|S_IRUGO,
  142. tle62x0_gpio_show, tle62x0_gpio_store);
  143. static DEVICE_ATTR(gpio3, S_IWUSR|S_IRUGO,
  144. tle62x0_gpio_show, tle62x0_gpio_store);
  145. static DEVICE_ATTR(gpio4, S_IWUSR|S_IRUGO,
  146. tle62x0_gpio_show, tle62x0_gpio_store);
  147. static DEVICE_ATTR(gpio5, S_IWUSR|S_IRUGO,
  148. tle62x0_gpio_show, tle62x0_gpio_store);
  149. static DEVICE_ATTR(gpio6, S_IWUSR|S_IRUGO,
  150. tle62x0_gpio_show, tle62x0_gpio_store);
  151. static DEVICE_ATTR(gpio7, S_IWUSR|S_IRUGO,
  152. tle62x0_gpio_show, tle62x0_gpio_store);
  153. static DEVICE_ATTR(gpio8, S_IWUSR|S_IRUGO,
  154. tle62x0_gpio_show, tle62x0_gpio_store);
  155. static DEVICE_ATTR(gpio9, S_IWUSR|S_IRUGO,
  156. tle62x0_gpio_show, tle62x0_gpio_store);
  157. static DEVICE_ATTR(gpio10, S_IWUSR|S_IRUGO,
  158. tle62x0_gpio_show, tle62x0_gpio_store);
  159. static DEVICE_ATTR(gpio11, S_IWUSR|S_IRUGO,
  160. tle62x0_gpio_show, tle62x0_gpio_store);
  161. static DEVICE_ATTR(gpio12, S_IWUSR|S_IRUGO,
  162. tle62x0_gpio_show, tle62x0_gpio_store);
  163. static DEVICE_ATTR(gpio13, S_IWUSR|S_IRUGO,
  164. tle62x0_gpio_show, tle62x0_gpio_store);
  165. static DEVICE_ATTR(gpio14, S_IWUSR|S_IRUGO,
  166. tle62x0_gpio_show, tle62x0_gpio_store);
  167. static DEVICE_ATTR(gpio15, S_IWUSR|S_IRUGO,
  168. tle62x0_gpio_show, tle62x0_gpio_store);
  169. static DEVICE_ATTR(gpio16, S_IWUSR|S_IRUGO,
  170. tle62x0_gpio_show, tle62x0_gpio_store);
  171. static struct device_attribute *gpio_attrs[] = {
  172. [0] = &dev_attr_gpio1,
  173. [1] = &dev_attr_gpio2,
  174. [2] = &dev_attr_gpio3,
  175. [3] = &dev_attr_gpio4,
  176. [4] = &dev_attr_gpio5,
  177. [5] = &dev_attr_gpio6,
  178. [6] = &dev_attr_gpio7,
  179. [7] = &dev_attr_gpio8,
  180. [8] = &dev_attr_gpio9,
  181. [9] = &dev_attr_gpio10,
  182. [10] = &dev_attr_gpio11,
  183. [11] = &dev_attr_gpio12,
  184. [12] = &dev_attr_gpio13,
  185. [13] = &dev_attr_gpio14,
  186. [14] = &dev_attr_gpio15,
  187. [15] = &dev_attr_gpio16
  188. };
  189. static int to_gpio_num(struct device_attribute *attr)
  190. {
  191. int ptr;
  192. for (ptr = 0; ptr < ARRAY_SIZE(gpio_attrs); ptr++) {
  193. if (gpio_attrs[ptr] == attr)
  194. return ptr;
  195. }
  196. return -1;
  197. }
  198. static int tle62x0_probe(struct spi_device *spi)
  199. {
  200. struct tle62x0_state *st;
  201. struct tle62x0_pdata *pdata;
  202. int ptr;
  203. int ret;
  204. pdata = dev_get_platdata(&spi->dev);
  205. if (pdata == NULL) {
  206. dev_err(&spi->dev, "no device data specified\n");
  207. return -EINVAL;
  208. }
  209. st = kzalloc(sizeof(struct tle62x0_state), GFP_KERNEL);
  210. if (st == NULL)
  211. return -ENOMEM;
  212. st->us = spi;
  213. st->nr_gpio = pdata->gpio_count;
  214. st->gpio_state = pdata->init_state;
  215. mutex_init(&st->lock);
  216. ret = device_create_file(&spi->dev, &dev_attr_status_show);
  217. if (ret) {
  218. dev_err(&spi->dev, "cannot create status attribute\n");
  219. goto err_status;
  220. }
  221. for (ptr = 0; ptr < pdata->gpio_count; ptr++) {
  222. ret = device_create_file(&spi->dev, gpio_attrs[ptr]);
  223. if (ret) {
  224. dev_err(&spi->dev, "cannot create gpio attribute\n");
  225. goto err_gpios;
  226. }
  227. }
  228. /* tle62x0_write(st); */
  229. spi_set_drvdata(spi, st);
  230. return 0;
  231. err_gpios:
  232. while (--ptr >= 0)
  233. device_remove_file(&spi->dev, gpio_attrs[ptr]);
  234. device_remove_file(&spi->dev, &dev_attr_status_show);
  235. err_status:
  236. kfree(st);
  237. return ret;
  238. }
  239. static int tle62x0_remove(struct spi_device *spi)
  240. {
  241. struct tle62x0_state *st = spi_get_drvdata(spi);
  242. int ptr;
  243. for (ptr = 0; ptr < st->nr_gpio; ptr++)
  244. device_remove_file(&spi->dev, gpio_attrs[ptr]);
  245. device_remove_file(&spi->dev, &dev_attr_status_show);
  246. kfree(st);
  247. return 0;
  248. }
  249. static struct spi_driver tle62x0_driver = {
  250. .driver = {
  251. .name = "tle62x0",
  252. },
  253. .probe = tle62x0_probe,
  254. .remove = tle62x0_remove,
  255. };
  256. module_spi_driver(tle62x0_driver);
  257. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  258. MODULE_DESCRIPTION("TLE62x0 SPI driver");
  259. MODULE_LICENSE("GPL v2");
  260. MODULE_ALIAS("spi:tle62x0");