i2c-taos-evm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Driver for the TAOS evaluation modules
  3. * These devices include an I2C master which can be controlled over the
  4. * serial port.
  5. *
  6. * Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/input.h>
  26. #include <linux/serio.h>
  27. #include <linux/init.h>
  28. #include <linux/i2c.h>
  29. #define TAOS_BUFFER_SIZE 63
  30. #define TAOS_STATE_INIT 0
  31. #define TAOS_STATE_IDLE 1
  32. #define TAOS_STATE_SEND 2
  33. #define TAOS_STATE_RECV 3
  34. #define TAOS_CMD_RESET 0x12
  35. static DECLARE_WAIT_QUEUE_HEAD(wq);
  36. struct taos_data {
  37. struct i2c_adapter adapter;
  38. struct i2c_client *client;
  39. int state;
  40. u8 addr; /* last used address */
  41. unsigned char buffer[TAOS_BUFFER_SIZE];
  42. unsigned int pos; /* position inside the buffer */
  43. };
  44. /* TAOS TSL2550 EVM */
  45. static struct i2c_board_info tsl2550_info = {
  46. I2C_BOARD_INFO("tsl2550", 0x39),
  47. };
  48. /* Instantiate i2c devices based on the adapter name */
  49. static struct i2c_client *taos_instantiate_device(struct i2c_adapter *adapter)
  50. {
  51. if (!strncmp(adapter->name, "TAOS TSL2550 EVM", 16)) {
  52. dev_info(&adapter->dev, "Instantiating device %s at 0x%02x\n",
  53. tsl2550_info.type, tsl2550_info.addr);
  54. return i2c_new_device(adapter, &tsl2550_info);
  55. }
  56. return NULL;
  57. }
  58. static int taos_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  59. unsigned short flags, char read_write, u8 command,
  60. int size, union i2c_smbus_data *data)
  61. {
  62. struct serio *serio = adapter->algo_data;
  63. struct taos_data *taos = serio_get_drvdata(serio);
  64. char *p;
  65. /* Encode our transaction. "@" is for the device address, "$" for the
  66. SMBus command and "#" for the data. */
  67. p = taos->buffer;
  68. /* The device remembers the last used address, no need to send it
  69. again if it's the same */
  70. if (addr != taos->addr)
  71. p += sprintf(p, "@%02X", addr);
  72. switch (size) {
  73. case I2C_SMBUS_BYTE:
  74. if (read_write == I2C_SMBUS_WRITE)
  75. sprintf(p, "$#%02X", command);
  76. else
  77. sprintf(p, "$");
  78. break;
  79. case I2C_SMBUS_BYTE_DATA:
  80. if (read_write == I2C_SMBUS_WRITE)
  81. sprintf(p, "$%02X#%02X", command, data->byte);
  82. else
  83. sprintf(p, "$%02X", command);
  84. break;
  85. default:
  86. dev_warn(&adapter->dev, "Unsupported transaction %d\n", size);
  87. return -EOPNOTSUPP;
  88. }
  89. /* Send the transaction to the TAOS EVM */
  90. dev_dbg(&adapter->dev, "Command buffer: %s\n", taos->buffer);
  91. taos->pos = 0;
  92. taos->state = TAOS_STATE_SEND;
  93. serio_write(serio, taos->buffer[0]);
  94. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  95. msecs_to_jiffies(250));
  96. if (taos->state != TAOS_STATE_IDLE) {
  97. dev_err(&adapter->dev, "Transaction failed "
  98. "(state=%d, pos=%d)\n", taos->state, taos->pos);
  99. taos->addr = 0;
  100. return -EIO;
  101. }
  102. taos->addr = addr;
  103. /* Start the transaction and read the answer */
  104. taos->pos = 0;
  105. taos->state = TAOS_STATE_RECV;
  106. serio_write(serio, read_write == I2C_SMBUS_WRITE ? '>' : '<');
  107. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  108. msecs_to_jiffies(150));
  109. if (taos->state != TAOS_STATE_IDLE
  110. || taos->pos != 6) {
  111. dev_err(&adapter->dev, "Transaction timeout (pos=%d)\n",
  112. taos->pos);
  113. return -EIO;
  114. }
  115. dev_dbg(&adapter->dev, "Answer buffer: %s\n", taos->buffer);
  116. /* Interpret the returned string */
  117. p = taos->buffer + 2;
  118. p[3] = '\0';
  119. if (!strcmp(p, "NAK"))
  120. return -ENODEV;
  121. if (read_write == I2C_SMBUS_WRITE) {
  122. if (!strcmp(p, "ACK"))
  123. return 0;
  124. } else {
  125. if (p[0] == 'x') {
  126. data->byte = simple_strtol(p + 1, NULL, 16);
  127. return 0;
  128. }
  129. }
  130. return -EIO;
  131. }
  132. static u32 taos_smbus_func(struct i2c_adapter *adapter)
  133. {
  134. return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA;
  135. }
  136. static const struct i2c_algorithm taos_algorithm = {
  137. .smbus_xfer = taos_smbus_xfer,
  138. .functionality = taos_smbus_func,
  139. };
  140. static irqreturn_t taos_interrupt(struct serio *serio, unsigned char data,
  141. unsigned int flags)
  142. {
  143. struct taos_data *taos = serio_get_drvdata(serio);
  144. switch (taos->state) {
  145. case TAOS_STATE_INIT:
  146. taos->buffer[taos->pos++] = data;
  147. if (data == ':'
  148. || taos->pos == TAOS_BUFFER_SIZE - 1) {
  149. taos->buffer[taos->pos] = '\0';
  150. taos->state = TAOS_STATE_IDLE;
  151. wake_up_interruptible(&wq);
  152. }
  153. break;
  154. case TAOS_STATE_SEND:
  155. if (taos->buffer[++taos->pos])
  156. serio_write(serio, taos->buffer[taos->pos]);
  157. else {
  158. taos->state = TAOS_STATE_IDLE;
  159. wake_up_interruptible(&wq);
  160. }
  161. break;
  162. case TAOS_STATE_RECV:
  163. taos->buffer[taos->pos++] = data;
  164. if (data == ']') {
  165. taos->buffer[taos->pos] = '\0';
  166. taos->state = TAOS_STATE_IDLE;
  167. wake_up_interruptible(&wq);
  168. }
  169. break;
  170. }
  171. return IRQ_HANDLED;
  172. }
  173. /* Extract the adapter name from the buffer received after reset.
  174. The buffer is modified and a pointer inside the buffer is returned. */
  175. static char *taos_adapter_name(char *buffer)
  176. {
  177. char *start, *end;
  178. start = strstr(buffer, "TAOS ");
  179. if (!start)
  180. return NULL;
  181. end = strchr(start, '\r');
  182. if (!end)
  183. return NULL;
  184. *end = '\0';
  185. return start;
  186. }
  187. static int taos_connect(struct serio *serio, struct serio_driver *drv)
  188. {
  189. struct taos_data *taos;
  190. struct i2c_adapter *adapter;
  191. char *name;
  192. int err;
  193. taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
  194. if (!taos) {
  195. err = -ENOMEM;
  196. goto exit;
  197. }
  198. taos->state = TAOS_STATE_INIT;
  199. serio_set_drvdata(serio, taos);
  200. err = serio_open(serio, drv);
  201. if (err)
  202. goto exit_kfree;
  203. adapter = &taos->adapter;
  204. adapter->owner = THIS_MODULE;
  205. adapter->algo = &taos_algorithm;
  206. adapter->algo_data = serio;
  207. adapter->dev.parent = &serio->dev;
  208. /* Reset the TAOS evaluation module to identify it */
  209. serio_write(serio, TAOS_CMD_RESET);
  210. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  211. msecs_to_jiffies(2000));
  212. if (taos->state != TAOS_STATE_IDLE) {
  213. err = -ENODEV;
  214. dev_dbg(&serio->dev, "TAOS EVM reset failed (state=%d, "
  215. "pos=%d)\n", taos->state, taos->pos);
  216. goto exit_close;
  217. }
  218. name = taos_adapter_name(taos->buffer);
  219. if (!name) {
  220. err = -ENODEV;
  221. dev_err(&serio->dev, "TAOS EVM identification failed\n");
  222. goto exit_close;
  223. }
  224. strlcpy(adapter->name, name, sizeof(adapter->name));
  225. err = i2c_add_adapter(adapter);
  226. if (err)
  227. goto exit_close;
  228. dev_dbg(&serio->dev, "Connected to TAOS EVM\n");
  229. taos->client = taos_instantiate_device(adapter);
  230. return 0;
  231. exit_close:
  232. serio_close(serio);
  233. exit_kfree:
  234. serio_set_drvdata(serio, NULL);
  235. kfree(taos);
  236. exit:
  237. return err;
  238. }
  239. static void taos_disconnect(struct serio *serio)
  240. {
  241. struct taos_data *taos = serio_get_drvdata(serio);
  242. if (taos->client)
  243. i2c_unregister_device(taos->client);
  244. i2c_del_adapter(&taos->adapter);
  245. serio_close(serio);
  246. serio_set_drvdata(serio, NULL);
  247. kfree(taos);
  248. dev_dbg(&serio->dev, "Disconnected from TAOS EVM\n");
  249. }
  250. static struct serio_device_id taos_serio_ids[] = {
  251. {
  252. .type = SERIO_RS232,
  253. .proto = SERIO_TAOSEVM,
  254. .id = SERIO_ANY,
  255. .extra = SERIO_ANY,
  256. },
  257. { 0 }
  258. };
  259. MODULE_DEVICE_TABLE(serio, taos_serio_ids);
  260. static struct serio_driver taos_drv = {
  261. .driver = {
  262. .name = "taos-evm",
  263. },
  264. .description = "TAOS evaluation module driver",
  265. .id_table = taos_serio_ids,
  266. .connect = taos_connect,
  267. .disconnect = taos_disconnect,
  268. .interrupt = taos_interrupt,
  269. };
  270. static int __init taos_init(void)
  271. {
  272. return serio_register_driver(&taos_drv);
  273. }
  274. static void __exit taos_exit(void)
  275. {
  276. serio_unregister_driver(&taos_drv);
  277. }
  278. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  279. MODULE_DESCRIPTION("TAOS evaluation module driver");
  280. MODULE_LICENSE("GPL");
  281. module_init(taos_init);
  282. module_exit(taos_exit);