i2c-taos-evm.c 8.0 KB

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