tle62x0.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/slab.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/spi/tle62x0.h>
  16. #define CMD_READ 0x00
  17. #define CMD_SET 0xff
  18. #define DIAG_NORMAL 0x03
  19. #define DIAG_OVERLOAD 0x02
  20. #define DIAG_OPEN 0x01
  21. #define DIAG_SHORTGND 0x00
  22. struct tle62x0_state {
  23. struct spi_device *us;
  24. struct mutex lock;
  25. unsigned int nr_gpio;
  26. unsigned int gpio_state;
  27. unsigned char tx_buff[4];
  28. unsigned char rx_buff[4];
  29. };
  30. static int to_gpio_num(struct device_attribute *attr);
  31. static inline int tle62x0_write(struct tle62x0_state *st)
  32. {
  33. unsigned char *buff = st->tx_buff;
  34. unsigned int gpio_state = st->gpio_state;
  35. buff[0] = CMD_SET;
  36. if (st->nr_gpio == 16) {
  37. buff[1] = gpio_state >> 8;
  38. buff[2] = gpio_state;
  39. } else {
  40. buff[1] = gpio_state;
  41. }
  42. dev_dbg(&st->us->dev, "buff %02x,%02x,%02x\n",
  43. buff[0], buff[1], buff[2]);
  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 __devinit 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 = spi->dev.platform_data;
  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. dev_err(&spi->dev, "no memory for device state\n");
  212. return -ENOMEM;
  213. }
  214. st->us = spi;
  215. st->nr_gpio = pdata->gpio_count;
  216. st->gpio_state = pdata->init_state;
  217. mutex_init(&st->lock);
  218. ret = device_create_file(&spi->dev, &dev_attr_status_show);
  219. if (ret) {
  220. dev_err(&spi->dev, "cannot create status attribute\n");
  221. goto err_status;
  222. }
  223. for (ptr = 0; ptr < pdata->gpio_count; ptr++) {
  224. ret = device_create_file(&spi->dev, gpio_attrs[ptr]);
  225. if (ret) {
  226. dev_err(&spi->dev, "cannot create gpio attribute\n");
  227. goto err_gpios;
  228. }
  229. }
  230. /* tle62x0_write(st); */
  231. spi_set_drvdata(spi, st);
  232. return 0;
  233. err_gpios:
  234. while (--ptr >= 0)
  235. device_remove_file(&spi->dev, gpio_attrs[ptr]);
  236. device_remove_file(&spi->dev, &dev_attr_status_show);
  237. err_status:
  238. kfree(st);
  239. return ret;
  240. }
  241. static int __devexit tle62x0_remove(struct spi_device *spi)
  242. {
  243. struct tle62x0_state *st = spi_get_drvdata(spi);
  244. int ptr;
  245. for (ptr = 0; ptr < st->nr_gpio; ptr++)
  246. device_remove_file(&spi->dev, gpio_attrs[ptr]);
  247. device_remove_file(&spi->dev, &dev_attr_status_show);
  248. kfree(st);
  249. return 0;
  250. }
  251. static struct spi_driver tle62x0_driver = {
  252. .driver = {
  253. .name = "tle62x0",
  254. .owner = THIS_MODULE,
  255. },
  256. .probe = tle62x0_probe,
  257. .remove = __devexit_p(tle62x0_remove),
  258. };
  259. static __init int tle62x0_init(void)
  260. {
  261. return spi_register_driver(&tle62x0_driver);
  262. }
  263. static __exit void tle62x0_exit(void)
  264. {
  265. spi_unregister_driver(&tle62x0_driver);
  266. }
  267. module_init(tle62x0_init);
  268. module_exit(tle62x0_exit);
  269. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  270. MODULE_DESCRIPTION("TLE62x0 SPI driver");
  271. MODULE_LICENSE("GPL v2");
  272. MODULE_ALIAS("spi:tle62x0");