i2c-taos-evm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_dbg(&adapter->dev, "Unsupported transaction size %d\n",
  87. size);
  88. return -EINVAL;
  89. }
  90. /* Send the transaction to the TAOS EVM */
  91. dev_dbg(&adapter->dev, "Command buffer: %s\n", taos->buffer);
  92. taos->pos = 0;
  93. taos->state = TAOS_STATE_SEND;
  94. serio_write(serio, taos->buffer[0]);
  95. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  96. msecs_to_jiffies(250));
  97. if (taos->state != TAOS_STATE_IDLE) {
  98. dev_err(&adapter->dev, "Transaction failed "
  99. "(state=%d, pos=%d)\n", taos->state, taos->pos);
  100. taos->addr = 0;
  101. return -EIO;
  102. }
  103. taos->addr = addr;
  104. /* Start the transaction and read the answer */
  105. taos->pos = 0;
  106. taos->state = TAOS_STATE_RECV;
  107. serio_write(serio, read_write == I2C_SMBUS_WRITE ? '>' : '<');
  108. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  109. msecs_to_jiffies(150));
  110. if (taos->state != TAOS_STATE_IDLE
  111. || taos->pos != 6) {
  112. dev_err(&adapter->dev, "Transaction timeout (pos=%d)\n",
  113. taos->pos);
  114. return -EIO;
  115. }
  116. dev_dbg(&adapter->dev, "Answer buffer: %s\n", taos->buffer);
  117. /* Interpret the returned string */
  118. p = taos->buffer + 2;
  119. p[3] = '\0';
  120. if (!strcmp(p, "NAK"))
  121. return -ENODEV;
  122. if (read_write == I2C_SMBUS_WRITE) {
  123. if (!strcmp(p, "ACK"))
  124. return 0;
  125. } else {
  126. if (p[0] == 'x') {
  127. data->byte = simple_strtol(p + 1, NULL, 16);
  128. return 0;
  129. }
  130. }
  131. return -EIO;
  132. }
  133. static u32 taos_smbus_func(struct i2c_adapter *adapter)
  134. {
  135. return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA;
  136. }
  137. static const struct i2c_algorithm taos_algorithm = {
  138. .smbus_xfer = taos_smbus_xfer,
  139. .functionality = taos_smbus_func,
  140. };
  141. static irqreturn_t taos_interrupt(struct serio *serio, unsigned char data,
  142. unsigned int flags)
  143. {
  144. struct taos_data *taos = serio_get_drvdata(serio);
  145. switch (taos->state) {
  146. case TAOS_STATE_INIT:
  147. taos->buffer[taos->pos++] = data;
  148. if (data == ':'
  149. || taos->pos == TAOS_BUFFER_SIZE - 1) {
  150. taos->buffer[taos->pos] = '\0';
  151. taos->state = TAOS_STATE_IDLE;
  152. wake_up_interruptible(&wq);
  153. }
  154. break;
  155. case TAOS_STATE_SEND:
  156. if (taos->buffer[++taos->pos])
  157. serio_write(serio, taos->buffer[taos->pos]);
  158. else {
  159. taos->state = TAOS_STATE_IDLE;
  160. wake_up_interruptible(&wq);
  161. }
  162. break;
  163. case TAOS_STATE_RECV:
  164. taos->buffer[taos->pos++] = data;
  165. if (data == ']') {
  166. taos->buffer[taos->pos] = '\0';
  167. taos->state = TAOS_STATE_IDLE;
  168. wake_up_interruptible(&wq);
  169. }
  170. break;
  171. }
  172. return IRQ_HANDLED;
  173. }
  174. /* Extract the adapter name from the buffer received after reset.
  175. The buffer is modified and a pointer inside the buffer is returned. */
  176. static char *taos_adapter_name(char *buffer)
  177. {
  178. char *start, *end;
  179. start = strstr(buffer, "TAOS ");
  180. if (!start)
  181. return NULL;
  182. end = strchr(start, '\r');
  183. if (!end)
  184. return NULL;
  185. *end = '\0';
  186. return start;
  187. }
  188. static int taos_connect(struct serio *serio, struct serio_driver *drv)
  189. {
  190. struct taos_data *taos;
  191. struct i2c_adapter *adapter;
  192. char *name;
  193. int err;
  194. taos = kzalloc(sizeof(struct taos_data), GFP_KERNEL);
  195. if (!taos) {
  196. err = -ENOMEM;
  197. goto exit;
  198. }
  199. taos->state = TAOS_STATE_INIT;
  200. serio_set_drvdata(serio, taos);
  201. err = serio_open(serio, drv);
  202. if (err)
  203. goto exit_kfree;
  204. adapter = &taos->adapter;
  205. adapter->owner = THIS_MODULE;
  206. adapter->algo = &taos_algorithm;
  207. adapter->algo_data = serio;
  208. adapter->dev.parent = &serio->dev;
  209. /* Reset the TAOS evaluation module to identify it */
  210. serio_write(serio, TAOS_CMD_RESET);
  211. wait_event_interruptible_timeout(wq, taos->state == TAOS_STATE_IDLE,
  212. msecs_to_jiffies(2000));
  213. if (taos->state != TAOS_STATE_IDLE) {
  214. err = -ENODEV;
  215. dev_dbg(&serio->dev, "TAOS EVM reset failed (state=%d, "
  216. "pos=%d)\n", taos->state, taos->pos);
  217. goto exit_close;
  218. }
  219. name = taos_adapter_name(taos->buffer);
  220. if (!name) {
  221. err = -ENODEV;
  222. dev_err(&serio->dev, "TAOS EVM identification failed\n");
  223. goto exit_close;
  224. }
  225. strlcpy(adapter->name, name, sizeof(adapter->name));
  226. err = i2c_add_adapter(adapter);
  227. if (err)
  228. goto exit_close;
  229. dev_dbg(&serio->dev, "Connected to TAOS EVM\n");
  230. taos->client = taos_instantiate_device(adapter);
  231. return 0;
  232. exit_close:
  233. serio_close(serio);
  234. exit_kfree:
  235. serio_set_drvdata(serio, NULL);
  236. kfree(taos);
  237. exit:
  238. return err;
  239. }
  240. static void taos_disconnect(struct serio *serio)
  241. {
  242. struct taos_data *taos = serio_get_drvdata(serio);
  243. if (taos->client)
  244. i2c_unregister_device(taos->client);
  245. i2c_del_adapter(&taos->adapter);
  246. serio_close(serio);
  247. serio_set_drvdata(serio, NULL);
  248. kfree(taos);
  249. dev_dbg(&serio->dev, "Disconnected from TAOS EVM\n");
  250. }
  251. static struct serio_device_id taos_serio_ids[] = {
  252. {
  253. .type = SERIO_RS232,
  254. .proto = SERIO_TAOSEVM,
  255. .id = SERIO_ANY,
  256. .extra = SERIO_ANY,
  257. },
  258. { 0 }
  259. };
  260. MODULE_DEVICE_TABLE(serio, taos_serio_ids);
  261. static struct serio_driver taos_drv = {
  262. .driver = {
  263. .name = "taos-evm",
  264. },
  265. .description = "TAOS evaluation module driver",
  266. .id_table = taos_serio_ids,
  267. .connect = taos_connect,
  268. .disconnect = taos_disconnect,
  269. .interrupt = taos_interrupt,
  270. };
  271. static int __init taos_init(void)
  272. {
  273. return serio_register_driver(&taos_drv);
  274. }
  275. static void __exit taos_exit(void)
  276. {
  277. serio_unregister_driver(&taos_drv);
  278. }
  279. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  280. MODULE_DESCRIPTION("TAOS evaluation module driver");
  281. MODULE_LICENSE("GPL");
  282. module_init(taos_init);
  283. module_exit(taos_exit);