i2c-taos-evm.c 8.0 KB

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