tle62x0.c 7.4 KB

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