i2c-taos-evm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <jdelvare@suse.de>
  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. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/input.h>
  22. #include <linux/serio.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #define TAOS_BUFFER_SIZE 63
  26. #define TAOS_STATE_INIT 0
  27. #define TAOS_STATE_IDLE 1
  28. #define TAOS_STATE_EOFF 2
  29. #define TAOS_STATE_RECV 3
  30. #define TAOS_CMD_RESET 0x12
  31. #define TAOS_CMD_ECHO_ON '+'
  32. #define TAOS_CMD_ECHO_OFF '-'
  33. static DECLARE_WAIT_QUEUE_HEAD(wq);
  34. struct taos_data {
  35. struct i2c_adapter adapter;
  36. struct i2c_client *client;
  37. int state;
  38. u8 addr; /* last used address */
  39. unsigned char buffer[TAOS_BUFFER_SIZE];
  40. unsigned int pos; /* position inside the buffer */
  41. };
  42. /* TAOS TSL2550 EVM */
  43. static struct i2c_board_info tsl2550_info = {
  44. I2C_BOARD_INFO("tsl2550", 0x39),
  45. };
  46. /* Instantiate i2c devices based on the adapter name */
  47. static struct i2c_client *taos_instantiate_device(struct i2c_adapter *adapter)
  48. {
  49. if (!strncmp(adapter->name, "TAOS TSL2550 EVM", 16)) {
  50. dev_info(&adapter->dev, "Instantiating device %s at 0x%02x\n",
  51. tsl2550_info.type, tsl2550_info.addr);
  52. return i2c_new_device(adapter, &tsl2550_info);
  53. }
  54. return NULL;
  55. }
  56. static int taos_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  57. unsigned short flags, char read_write, u8 command,
  58. int size, union i2c_smbus_data *data)
  59. {
  60. struct serio *serio = adapter->algo_data;
  61. struct taos_data *taos = serio_get_drvdata(serio);
  62. char *p;
  63. /* Encode our transaction. "@" is for the device address, "$" for the
  64. SMBus command and "#" for the data. */
  65. p = taos->buffer;
  66. /* The device remembers the last used address, no need to send it
  67. again if it's the same */
  68. if (addr != taos->addr)
  69. p += sprintf(p, "@%02X", addr);
  70. switch (size) {
  71. case I2C_SMBUS_BYTE:
  72. if (read_write == I2C_SMBUS_WRITE)
  73. sprintf(p, "$#%02X", command);
  74. else
  75. sprintf(p, "$");
  76. break;
  77. case I2C_SMBUS_BYTE_DATA:
  78. if (read_write == I2C_SMBUS_WRITE)
  79. sprintf(p, "$%02X#%02X", command, data->byte);
  80. else
  81. sprintf(p, "$%02X", command);
  82. break;
  83. default:
  84. dev_warn(&adapter->dev, "Unsupported transaction %d\n", size);
  85. return -EOPNOTSUPP;
  86. }
  87. /* Send the transaction to the TAOS EVM */
  88. dev_dbg(&adapter->dev, "Command buffer: %s\n", taos->buffer);
  89. for (p = taos->buffer; *p; p++)
  90. serio_write(serio, *p);
  91. taos->addr = addr;
  92. /* Start the transaction and read the answer */
  93. taos->pos = 0;
  94. taos->state = TAOS_STATE_RECV;
  95. serio_write(serio, read_write == I2C_SMBUS_WRITE ? '>' : '<');
  96. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  97. msecs_to_jiffies(150));
  98. if (taos->state != TAOS_STATE_IDLE
  99. || taos->pos != 5) {
  100. dev_err(&adapter->dev, "Transaction timeout (pos=%d)\n",
  101. taos->pos);
  102. return -EIO;
  103. }
  104. dev_dbg(&adapter->dev, "Answer buffer: %s\n", taos->buffer);
  105. /* Interpret the returned string */
  106. p = taos->buffer + 1;
  107. p[3] = '\0';
  108. if (!strcmp(p, "NAK"))
  109. return -ENODEV;
  110. if (read_write == I2C_SMBUS_WRITE) {
  111. if (!strcmp(p, "ACK"))
  112. return 0;
  113. } else {
  114. if (p[0] == 'x') {
  115. data->byte = simple_strtol(p + 1, NULL, 16);
  116. return 0;
  117. }
  118. }
  119. return -EIO;
  120. }
  121. static u32 taos_smbus_func(struct i2c_adapter *adapter)
  122. {
  123. return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA;
  124. }
  125. static const struct i2c_algorithm taos_algorithm = {
  126. .smbus_xfer = taos_smbus_xfer,
  127. .functionality = taos_smbus_func,
  128. };
  129. static irqreturn_t taos_interrupt(struct serio *serio, unsigned char data,
  130. unsigned int flags)
  131. {
  132. struct taos_data *taos = serio_get_drvdata(serio);
  133. switch (taos->state) {
  134. case TAOS_STATE_INIT:
  135. taos->buffer[taos->pos++] = data;
  136. if (data == ':'
  137. || taos->pos == TAOS_BUFFER_SIZE - 1) {
  138. taos->buffer[taos->pos] = '\0';
  139. taos->state = TAOS_STATE_IDLE;
  140. wake_up_interruptible(&wq);
  141. }
  142. break;
  143. case TAOS_STATE_EOFF:
  144. taos->state = TAOS_STATE_IDLE;
  145. wake_up_interruptible(&wq);
  146. break;
  147. case TAOS_STATE_RECV:
  148. taos->buffer[taos->pos++] = data;
  149. if (data == ']') {
  150. taos->buffer[taos->pos] = '\0';
  151. taos->state = TAOS_STATE_IDLE;
  152. wake_up_interruptible(&wq);
  153. }
  154. break;
  155. }
  156. return IRQ_HANDLED;
  157. }
  158. /* Extract the adapter name from the buffer received after reset.
  159. The buffer is modified and a pointer inside the buffer is returned. */
  160. static char *taos_adapter_name(char *buffer)
  161. {
  162. char *start, *end;
  163. start = strstr(buffer, "TAOS ");
  164. if (!start)
  165. return NULL;
  166. end = strchr(start, '\r');
  167. if (!end)
  168. return NULL;
  169. *end = '\0';
  170. return start;
  171. }
  172. static int taos_connect(struct serio *serio, struct serio_driver *drv)
  173. {
  174. struct taos_data *taos;
  175. struct i2c_adapter *adapter;
  176. char *name;
  177. int err;
  178. taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
  179. if (!taos) {
  180. err = -ENOMEM;
  181. goto exit;
  182. }
  183. taos->state = TAOS_STATE_INIT;
  184. serio_set_drvdata(serio, taos);
  185. err = serio_open(serio, drv);
  186. if (err)
  187. goto exit_kfree;
  188. adapter = &taos->adapter;
  189. adapter->owner = THIS_MODULE;
  190. adapter->algo = &taos_algorithm;
  191. adapter->algo_data = serio;
  192. adapter->dev.parent = &serio->dev;
  193. /* Reset the TAOS evaluation module to identify it */
  194. serio_write(serio, TAOS_CMD_RESET);
  195. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  196. msecs_to_jiffies(2000));
  197. if (taos->state != TAOS_STATE_IDLE) {
  198. err = -ENODEV;
  199. dev_err(&serio->dev, "TAOS EVM reset failed (state=%d, "
  200. "pos=%d)\n", taos->state, taos->pos);
  201. goto exit_close;
  202. }
  203. name = taos_adapter_name(taos->buffer);
  204. if (!name) {
  205. err = -ENODEV;
  206. dev_err(&serio->dev, "TAOS EVM identification failed\n");
  207. goto exit_close;
  208. }
  209. strlcpy(adapter->name, name, sizeof(adapter->name));
  210. /* Turn echo off for better performance */
  211. taos->state = TAOS_STATE_EOFF;
  212. serio_write(serio, TAOS_CMD_ECHO_OFF);
  213. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  214. msecs_to_jiffies(250));
  215. if (taos->state != TAOS_STATE_IDLE) {
  216. err = -ENODEV;
  217. dev_err(&serio->dev, "TAOS EVM echo off failed "
  218. "(state=%d)\n", taos->state);
  219. goto exit_close;
  220. }
  221. err = i2c_add_adapter(adapter);
  222. if (err)
  223. goto exit_close;
  224. dev_info(&serio->dev, "Connected to TAOS EVM\n");
  225. taos->client = taos_instantiate_device(adapter);
  226. return 0;
  227. exit_close:
  228. serio_close(serio);
  229. exit_kfree:
  230. kfree(taos);
  231. exit:
  232. return err;
  233. }
  234. static void taos_disconnect(struct serio *serio)
  235. {
  236. struct taos_data *taos = serio_get_drvdata(serio);
  237. if (taos->client)
  238. i2c_unregister_device(taos->client);
  239. i2c_del_adapter(&taos->adapter);
  240. serio_close(serio);
  241. kfree(taos);
  242. dev_info(&serio->dev, "Disconnected from TAOS EVM\n");
  243. }
  244. static struct serio_device_id taos_serio_ids[] = {
  245. {
  246. .type = SERIO_RS232,
  247. .proto = SERIO_TAOSEVM,
  248. .id = SERIO_ANY,
  249. .extra = SERIO_ANY,
  250. },
  251. { 0 }
  252. };
  253. MODULE_DEVICE_TABLE(serio, taos_serio_ids);
  254. static struct serio_driver taos_drv = {
  255. .driver = {
  256. .name = "taos-evm",
  257. },
  258. .description = "TAOS evaluation module driver",
  259. .id_table = taos_serio_ids,
  260. .connect = taos_connect,
  261. .disconnect = taos_disconnect,
  262. .interrupt = taos_interrupt,
  263. };
  264. module_serio_driver(taos_drv);
  265. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  266. MODULE_DESCRIPTION("TAOS evaluation module driver");
  267. MODULE_LICENSE("GPL");