st_kim.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * Shared Transport Line discipline driver Core
  3. * Init Manager module responsible for GPIO control
  4. * and firmware download
  5. * Copyright (C) 2009-2010 Texas Instruments
  6. * Author: Pavan Savoy <pavan_savoy@ti.com>
  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 version 2 as
  10. * published by the Free Software Foundation.
  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. */
  22. #define pr_fmt(fmt) "(stk) :" fmt
  23. #include <linux/platform_device.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/firmware.h>
  26. #include <linux/delay.h>
  27. #include <linux/wait.h>
  28. #include <linux/gpio.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/sched.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/tty.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/ti_wilink_st.h>
  36. #include <linux/module.h>
  37. #include <linux/of.h>
  38. #include <linux/of_device.h>
  39. #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
  40. static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
  41. /**********************************************************************/
  42. /* internal functions */
  43. struct ti_st_plat_data *dt_pdata;
  44. static struct ti_st_plat_data *get_platform_data(struct device *dev);
  45. /**
  46. * st_get_plat_device -
  47. * function which returns the reference to the platform device
  48. * requested by id. As of now only 1 such device exists (id=0)
  49. * the context requesting for reference can get the id to be
  50. * requested by a. The protocol driver which is registering or
  51. * b. the tty device which is opened.
  52. */
  53. static struct platform_device *st_get_plat_device(int id)
  54. {
  55. return st_kim_devices[id];
  56. }
  57. /**
  58. * validate_firmware_response -
  59. * function to return whether the firmware response was proper
  60. * in case of error don't complete so that waiting for proper
  61. * response times out
  62. */
  63. static void validate_firmware_response(struct kim_data_s *kim_gdata)
  64. {
  65. struct sk_buff *skb = kim_gdata->rx_skb;
  66. if (!skb)
  67. return;
  68. /* these magic numbers are the position in the response buffer which
  69. * allows us to distinguish whether the response is for the read
  70. * version info. command
  71. */
  72. if (skb->data[2] == 0x01 && skb->data[3] == 0x01 &&
  73. skb->data[4] == 0x10 && skb->data[5] == 0x00) {
  74. /* fw version response */
  75. memcpy(kim_gdata->resp_buffer,
  76. kim_gdata->rx_skb->data,
  77. kim_gdata->rx_skb->len);
  78. complete_all(&kim_gdata->kim_rcvd);
  79. kim_gdata->rx_state = ST_W4_PACKET_TYPE;
  80. kim_gdata->rx_skb = NULL;
  81. kim_gdata->rx_count = 0;
  82. } else if (unlikely(skb->data[5] != 0)) {
  83. pr_err("no proper response during fw download");
  84. pr_err("data6 %x", skb->data[5]);
  85. kfree_skb(skb);
  86. return; /* keep waiting for the proper response */
  87. }
  88. /* becos of all the script being downloaded */
  89. complete_all(&kim_gdata->kim_rcvd);
  90. kfree_skb(skb);
  91. }
  92. /* check for data len received inside kim_int_recv
  93. * most often hit the last case to update state to waiting for data
  94. */
  95. static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
  96. {
  97. register int room = skb_tailroom(kim_gdata->rx_skb);
  98. pr_debug("len %d room %d", len, room);
  99. if (!len) {
  100. validate_firmware_response(kim_gdata);
  101. } else if (len > room) {
  102. /* Received packet's payload length is larger.
  103. * We can't accommodate it in created skb.
  104. */
  105. pr_err("Data length is too large len %d room %d", len,
  106. room);
  107. kfree_skb(kim_gdata->rx_skb);
  108. } else {
  109. /* Packet header has non-zero payload length and
  110. * we have enough space in created skb. Lets read
  111. * payload data */
  112. kim_gdata->rx_state = ST_W4_DATA;
  113. kim_gdata->rx_count = len;
  114. return len;
  115. }
  116. /* Change ST LL state to continue to process next
  117. * packet */
  118. kim_gdata->rx_state = ST_W4_PACKET_TYPE;
  119. kim_gdata->rx_skb = NULL;
  120. kim_gdata->rx_count = 0;
  121. return 0;
  122. }
  123. /**
  124. * kim_int_recv - receive function called during firmware download
  125. * firmware download responses on different UART drivers
  126. * have been observed to come in bursts of different
  127. * tty_receive and hence the logic
  128. */
  129. static void kim_int_recv(struct kim_data_s *kim_gdata,
  130. const unsigned char *data, long count)
  131. {
  132. const unsigned char *ptr;
  133. int len = 0, type = 0;
  134. unsigned char *plen;
  135. pr_debug("%s", __func__);
  136. /* Decode received bytes here */
  137. ptr = data;
  138. if (unlikely(ptr == NULL)) {
  139. pr_err(" received null from TTY ");
  140. return;
  141. }
  142. while (count) {
  143. if (kim_gdata->rx_count) {
  144. len = min_t(unsigned int, kim_gdata->rx_count, count);
  145. memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
  146. kim_gdata->rx_count -= len;
  147. count -= len;
  148. ptr += len;
  149. if (kim_gdata->rx_count)
  150. continue;
  151. /* Check ST RX state machine , where are we? */
  152. switch (kim_gdata->rx_state) {
  153. /* Waiting for complete packet ? */
  154. case ST_W4_DATA:
  155. pr_debug("Complete pkt received");
  156. validate_firmware_response(kim_gdata);
  157. kim_gdata->rx_state = ST_W4_PACKET_TYPE;
  158. kim_gdata->rx_skb = NULL;
  159. continue;
  160. /* Waiting for Bluetooth event header ? */
  161. case ST_W4_HEADER:
  162. plen =
  163. (unsigned char *)&kim_gdata->rx_skb->data[1];
  164. pr_debug("event hdr: plen 0x%02x\n", *plen);
  165. kim_check_data_len(kim_gdata, *plen);
  166. continue;
  167. } /* end of switch */
  168. } /* end of if rx_state */
  169. switch (*ptr) {
  170. /* Bluetooth event packet? */
  171. case 0x04:
  172. kim_gdata->rx_state = ST_W4_HEADER;
  173. kim_gdata->rx_count = 2;
  174. type = *ptr;
  175. break;
  176. default:
  177. pr_info("unknown packet");
  178. ptr++;
  179. count--;
  180. continue;
  181. }
  182. ptr++;
  183. count--;
  184. kim_gdata->rx_skb =
  185. alloc_skb(1024+8, GFP_ATOMIC);
  186. if (!kim_gdata->rx_skb) {
  187. pr_err("can't allocate mem for new packet");
  188. kim_gdata->rx_state = ST_W4_PACKET_TYPE;
  189. kim_gdata->rx_count = 0;
  190. return;
  191. }
  192. skb_reserve(kim_gdata->rx_skb, 8);
  193. kim_gdata->rx_skb->cb[0] = 4;
  194. kim_gdata->rx_skb->cb[1] = 0;
  195. }
  196. return;
  197. }
  198. static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
  199. {
  200. unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
  201. const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
  202. long timeout;
  203. pr_debug("%s", __func__);
  204. reinit_completion(&kim_gdata->kim_rcvd);
  205. if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
  206. pr_err("kim: couldn't write 4 bytes");
  207. return -EIO;
  208. }
  209. timeout = wait_for_completion_interruptible_timeout(
  210. &kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME));
  211. if (timeout <= 0) {
  212. pr_err(" waiting for ver info- timed out or received signal");
  213. return timeout ? -ERESTARTSYS : -ETIMEDOUT;
  214. }
  215. reinit_completion(&kim_gdata->kim_rcvd);
  216. /* the positions 12 & 13 in the response buffer provide with the
  217. * chip, major & minor numbers
  218. */
  219. version =
  220. MAKEWORD(kim_gdata->resp_buffer[12],
  221. kim_gdata->resp_buffer[13]);
  222. chip = (version & 0x7C00) >> 10;
  223. min_ver = (version & 0x007F);
  224. maj_ver = (version & 0x0380) >> 7;
  225. if (version & 0x8000)
  226. maj_ver |= 0x0008;
  227. sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts",
  228. chip, maj_ver, min_ver);
  229. /* to be accessed later via sysfs entry */
  230. kim_gdata->version.full = version;
  231. kim_gdata->version.chip = chip;
  232. kim_gdata->version.maj_ver = maj_ver;
  233. kim_gdata->version.min_ver = min_ver;
  234. pr_info("%s", bts_scr_name);
  235. return 0;
  236. }
  237. static void skip_change_remote_baud(unsigned char **ptr, long *len)
  238. {
  239. unsigned char *nxt_action, *cur_action;
  240. cur_action = *ptr;
  241. nxt_action = cur_action + sizeof(struct bts_action) +
  242. ((struct bts_action *) cur_action)->size;
  243. if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
  244. pr_err("invalid action after change remote baud command");
  245. } else {
  246. *ptr = *ptr + sizeof(struct bts_action) +
  247. ((struct bts_action *)cur_action)->size;
  248. *len = *len - (sizeof(struct bts_action) +
  249. ((struct bts_action *)cur_action)->size);
  250. /* warn user on not commenting these in firmware */
  251. pr_warn("skipping the wait event of change remote baud");
  252. }
  253. }
  254. /**
  255. * download_firmware -
  256. * internal function which parses through the .bts firmware
  257. * script file intreprets SEND, DELAY actions only as of now
  258. */
  259. static long download_firmware(struct kim_data_s *kim_gdata)
  260. {
  261. long err = 0;
  262. long len = 0;
  263. unsigned char *ptr = NULL;
  264. unsigned char *action_ptr = NULL;
  265. unsigned char bts_scr_name[40] = { 0 }; /* 40 char long bts scr name? */
  266. int wr_room_space;
  267. int cmd_size;
  268. unsigned long timeout;
  269. err = read_local_version(kim_gdata, bts_scr_name);
  270. if (err != 0) {
  271. pr_err("kim: failed to read local ver");
  272. return err;
  273. }
  274. err =
  275. request_firmware(&kim_gdata->fw_entry, bts_scr_name,
  276. &kim_gdata->kim_pdev->dev);
  277. if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
  278. (kim_gdata->fw_entry->size == 0))) {
  279. pr_err(" request_firmware failed(errno %ld) for %s", err,
  280. bts_scr_name);
  281. return -EINVAL;
  282. }
  283. ptr = (void *)kim_gdata->fw_entry->data;
  284. len = kim_gdata->fw_entry->size;
  285. /* bts_header to remove out magic number and
  286. * version
  287. */
  288. ptr += sizeof(struct bts_header);
  289. len -= sizeof(struct bts_header);
  290. while (len > 0 && ptr) {
  291. pr_debug(" action size %d, type %d ",
  292. ((struct bts_action *)ptr)->size,
  293. ((struct bts_action *)ptr)->type);
  294. switch (((struct bts_action *)ptr)->type) {
  295. case ACTION_SEND_COMMAND: /* action send */
  296. pr_debug("S");
  297. action_ptr = &(((struct bts_action *)ptr)->data[0]);
  298. if (unlikely
  299. (((struct hci_command *)action_ptr)->opcode ==
  300. 0xFF36)) {
  301. /* ignore remote change
  302. * baud rate HCI VS command */
  303. pr_warn("change remote baud"
  304. " rate command in firmware");
  305. skip_change_remote_baud(&ptr, &len);
  306. break;
  307. }
  308. /*
  309. * Make sure we have enough free space in uart
  310. * tx buffer to write current firmware command
  311. */
  312. cmd_size = ((struct bts_action *)ptr)->size;
  313. timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
  314. do {
  315. wr_room_space =
  316. st_get_uart_wr_room(kim_gdata->core_data);
  317. if (wr_room_space < 0) {
  318. pr_err("Unable to get free "
  319. "space info from uart tx buffer");
  320. release_firmware(kim_gdata->fw_entry);
  321. return wr_room_space;
  322. }
  323. mdelay(1); /* wait 1ms before checking room */
  324. } while ((wr_room_space < cmd_size) &&
  325. time_before(jiffies, timeout));
  326. /* Timeout happened ? */
  327. if (time_after_eq(jiffies, timeout)) {
  328. pr_err("Timeout while waiting for free "
  329. "free space in uart tx buffer");
  330. release_firmware(kim_gdata->fw_entry);
  331. return -ETIMEDOUT;
  332. }
  333. /* reinit completion before sending for the
  334. * relevant wait
  335. */
  336. reinit_completion(&kim_gdata->kim_rcvd);
  337. /*
  338. * Free space found in uart buffer, call st_int_write
  339. * to send current firmware command to the uart tx
  340. * buffer.
  341. */
  342. err = st_int_write(kim_gdata->core_data,
  343. ((struct bts_action_send *)action_ptr)->data,
  344. ((struct bts_action *)ptr)->size);
  345. if (unlikely(err < 0)) {
  346. release_firmware(kim_gdata->fw_entry);
  347. return err;
  348. }
  349. /*
  350. * Check number of bytes written to the uart tx buffer
  351. * and requested command write size
  352. */
  353. if (err != cmd_size) {
  354. pr_err("Number of bytes written to uart "
  355. "tx buffer are not matching with "
  356. "requested cmd write size");
  357. release_firmware(kim_gdata->fw_entry);
  358. return -EIO;
  359. }
  360. break;
  361. case ACTION_WAIT_EVENT: /* wait */
  362. pr_debug("W");
  363. err = wait_for_completion_interruptible_timeout(
  364. &kim_gdata->kim_rcvd,
  365. msecs_to_jiffies(CMD_RESP_TIME));
  366. if (err <= 0) {
  367. pr_err("response timeout/signaled during fw download ");
  368. /* timed out */
  369. release_firmware(kim_gdata->fw_entry);
  370. return err ? -ERESTARTSYS : -ETIMEDOUT;
  371. }
  372. reinit_completion(&kim_gdata->kim_rcvd);
  373. break;
  374. case ACTION_DELAY: /* sleep */
  375. pr_info("sleep command in scr");
  376. action_ptr = &(((struct bts_action *)ptr)->data[0]);
  377. mdelay(((struct bts_action_delay *)action_ptr)->msec);
  378. break;
  379. }
  380. len =
  381. len - (sizeof(struct bts_action) +
  382. ((struct bts_action *)ptr)->size);
  383. ptr =
  384. ptr + sizeof(struct bts_action) +
  385. ((struct bts_action *)ptr)->size;
  386. }
  387. /* fw download complete */
  388. release_firmware(kim_gdata->fw_entry);
  389. return 0;
  390. }
  391. /**********************************************************************/
  392. /* functions called from ST core */
  393. /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
  394. * can be because of
  395. * 1. response to read local version
  396. * 2. during send/recv's of firmware download
  397. */
  398. void st_kim_recv(void *disc_data, const unsigned char *data, long count)
  399. {
  400. struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
  401. struct kim_data_s *kim_gdata = st_gdata->kim_data;
  402. /* proceed to gather all data and distinguish read fw version response
  403. * from other fw responses when data gathering is complete
  404. */
  405. kim_int_recv(kim_gdata, data, count);
  406. return;
  407. }
  408. /* to signal completion of line discipline installation
  409. * called from ST Core, upon tty_open
  410. */
  411. void st_kim_complete(void *kim_data)
  412. {
  413. struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
  414. complete(&kim_gdata->ldisc_installed);
  415. }
  416. /**
  417. * st_kim_start - called from ST Core upon 1st registration
  418. * This involves toggling the chip enable gpio, reading
  419. * the firmware version from chip, forming the fw file name
  420. * based on the chip version, requesting the fw, parsing it
  421. * and perform download(send/recv).
  422. */
  423. long st_kim_start(void *kim_data)
  424. {
  425. long err = 0;
  426. long retry = POR_RETRY_COUNT;
  427. struct ti_st_plat_data *pdata;
  428. struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
  429. pr_info(" %s", __func__);
  430. if (kim_gdata->kim_pdev->dev.of_node) {
  431. pr_debug("use device tree data");
  432. pdata = dt_pdata;
  433. } else {
  434. pdata = kim_gdata->kim_pdev->dev.platform_data;
  435. }
  436. do {
  437. /* platform specific enabling code here */
  438. if (pdata->chip_enable)
  439. pdata->chip_enable(kim_gdata);
  440. /* Configure BT nShutdown to HIGH state */
  441. gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
  442. mdelay(5); /* FIXME: a proper toggle */
  443. gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
  444. mdelay(100);
  445. /* re-initialize the completion */
  446. reinit_completion(&kim_gdata->ldisc_installed);
  447. /* send notification to UIM */
  448. kim_gdata->ldisc_install = 1;
  449. pr_info("ldisc_install = 1");
  450. sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
  451. NULL, "install");
  452. /* wait for ldisc to be installed */
  453. err = wait_for_completion_interruptible_timeout(
  454. &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
  455. if (!err) {
  456. /* ldisc installation timeout,
  457. * flush uart, power cycle BT_EN */
  458. pr_err("ldisc installation timeout");
  459. err = st_kim_stop(kim_gdata);
  460. continue;
  461. } else {
  462. /* ldisc installed now */
  463. pr_info("line discipline installed");
  464. err = download_firmware(kim_gdata);
  465. if (err != 0) {
  466. /* ldisc installed but fw download failed,
  467. * flush uart & power cycle BT_EN */
  468. pr_err("download firmware failed");
  469. err = st_kim_stop(kim_gdata);
  470. continue;
  471. } else { /* on success don't retry */
  472. break;
  473. }
  474. }
  475. } while (retry--);
  476. return err;
  477. }
  478. /**
  479. * st_kim_stop - stop communication with chip.
  480. * This can be called from ST Core/KIM, on the-
  481. * (a) last un-register when chip need not be powered there-after,
  482. * (b) upon failure to either install ldisc or download firmware.
  483. * The function is responsible to (a) notify UIM about un-installation,
  484. * (b) flush UART if the ldisc was installed.
  485. * (c) reset BT_EN - pull down nshutdown at the end.
  486. * (d) invoke platform's chip disabling routine.
  487. */
  488. long st_kim_stop(void *kim_data)
  489. {
  490. long err = 0;
  491. struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
  492. struct ti_st_plat_data *pdata;
  493. struct tty_struct *tty = kim_gdata->core_data->tty;
  494. reinit_completion(&kim_gdata->ldisc_installed);
  495. if (kim_gdata->kim_pdev->dev.of_node) {
  496. pr_debug("use device tree data");
  497. pdata = dt_pdata;
  498. } else
  499. pdata = kim_gdata->kim_pdev->dev.platform_data;
  500. if (tty) { /* can be called before ldisc is installed */
  501. /* Flush any pending characters in the driver and discipline. */
  502. tty_ldisc_flush(tty);
  503. tty_driver_flush_buffer(tty);
  504. }
  505. /* send uninstall notification to UIM */
  506. pr_info("ldisc_install = 0");
  507. kim_gdata->ldisc_install = 0;
  508. sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
  509. /* wait for ldisc to be un-installed */
  510. err = wait_for_completion_interruptible_timeout(
  511. &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
  512. if (!err) { /* timeout */
  513. pr_err(" timed out waiting for ldisc to be un-installed");
  514. err = -ETIMEDOUT;
  515. }
  516. /* By default configure BT nShutdown to LOW state */
  517. gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
  518. mdelay(1);
  519. gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
  520. mdelay(1);
  521. gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
  522. /* platform specific disable */
  523. if (pdata->chip_disable)
  524. pdata->chip_disable(kim_gdata);
  525. return err;
  526. }
  527. /**********************************************************************/
  528. /* functions called from subsystems */
  529. /* called when debugfs entry is read from */
  530. static int show_version(struct seq_file *s, void *unused)
  531. {
  532. struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
  533. seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
  534. kim_gdata->version.chip, kim_gdata->version.maj_ver,
  535. kim_gdata->version.min_ver);
  536. return 0;
  537. }
  538. static int show_list(struct seq_file *s, void *unused)
  539. {
  540. struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
  541. kim_st_list_protocols(kim_gdata->core_data, s);
  542. return 0;
  543. }
  544. static ssize_t show_install(struct device *dev,
  545. struct device_attribute *attr, char *buf)
  546. {
  547. struct kim_data_s *kim_data = dev_get_drvdata(dev);
  548. return sprintf(buf, "%d\n", kim_data->ldisc_install);
  549. }
  550. #ifdef DEBUG
  551. static ssize_t store_dev_name(struct device *dev,
  552. struct device_attribute *attr, const char *buf, size_t count)
  553. {
  554. struct kim_data_s *kim_data = dev_get_drvdata(dev);
  555. pr_debug("storing dev name >%s<", buf);
  556. strncpy(kim_data->dev_name, buf, count);
  557. pr_debug("stored dev name >%s<", kim_data->dev_name);
  558. return count;
  559. }
  560. static ssize_t store_baud_rate(struct device *dev,
  561. struct device_attribute *attr, const char *buf, size_t count)
  562. {
  563. struct kim_data_s *kim_data = dev_get_drvdata(dev);
  564. pr_debug("storing baud rate >%s<", buf);
  565. sscanf(buf, "%ld", &kim_data->baud_rate);
  566. pr_debug("stored baud rate >%ld<", kim_data->baud_rate);
  567. return count;
  568. }
  569. #endif /* if DEBUG */
  570. static ssize_t show_dev_name(struct device *dev,
  571. struct device_attribute *attr, char *buf)
  572. {
  573. struct kim_data_s *kim_data = dev_get_drvdata(dev);
  574. return sprintf(buf, "%s\n", kim_data->dev_name);
  575. }
  576. static ssize_t show_baud_rate(struct device *dev,
  577. struct device_attribute *attr, char *buf)
  578. {
  579. struct kim_data_s *kim_data = dev_get_drvdata(dev);
  580. return sprintf(buf, "%d\n", kim_data->baud_rate);
  581. }
  582. static ssize_t show_flow_cntrl(struct device *dev,
  583. struct device_attribute *attr, char *buf)
  584. {
  585. struct kim_data_s *kim_data = dev_get_drvdata(dev);
  586. return sprintf(buf, "%d\n", kim_data->flow_cntrl);
  587. }
  588. /* structures specific for sysfs entries */
  589. static struct kobj_attribute ldisc_install =
  590. __ATTR(install, 0444, (void *)show_install, NULL);
  591. static struct kobj_attribute uart_dev_name =
  592. #ifdef DEBUG /* TODO: move this to debug-fs if possible */
  593. __ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name);
  594. #else
  595. __ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
  596. #endif
  597. static struct kobj_attribute uart_baud_rate =
  598. #ifdef DEBUG /* TODO: move to debugfs */
  599. __ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate);
  600. #else
  601. __ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
  602. #endif
  603. static struct kobj_attribute uart_flow_cntrl =
  604. __ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
  605. static struct attribute *uim_attrs[] = {
  606. &ldisc_install.attr,
  607. &uart_dev_name.attr,
  608. &uart_baud_rate.attr,
  609. &uart_flow_cntrl.attr,
  610. NULL,
  611. };
  612. static struct attribute_group uim_attr_grp = {
  613. .attrs = uim_attrs,
  614. };
  615. /**
  616. * st_kim_ref - reference the core's data
  617. * This references the per-ST platform device in the arch/xx/
  618. * board-xx.c file.
  619. * This would enable multiple such platform devices to exist
  620. * on a given platform
  621. */
  622. void st_kim_ref(struct st_data_s **core_data, int id)
  623. {
  624. struct platform_device *pdev;
  625. struct kim_data_s *kim_gdata;
  626. /* get kim_gdata reference from platform device */
  627. pdev = st_get_plat_device(id);
  628. if (!pdev)
  629. goto err;
  630. kim_gdata = platform_get_drvdata(pdev);
  631. if (!kim_gdata)
  632. goto err;
  633. *core_data = kim_gdata->core_data;
  634. return;
  635. err:
  636. *core_data = NULL;
  637. }
  638. static int kim_version_open(struct inode *i, struct file *f)
  639. {
  640. return single_open(f, show_version, i->i_private);
  641. }
  642. static int kim_list_open(struct inode *i, struct file *f)
  643. {
  644. return single_open(f, show_list, i->i_private);
  645. }
  646. static const struct file_operations version_debugfs_fops = {
  647. /* version info */
  648. .open = kim_version_open,
  649. .read = seq_read,
  650. .llseek = seq_lseek,
  651. .release = single_release,
  652. };
  653. static const struct file_operations list_debugfs_fops = {
  654. /* protocols info */
  655. .open = kim_list_open,
  656. .read = seq_read,
  657. .llseek = seq_lseek,
  658. .release = single_release,
  659. };
  660. /**********************************************************************/
  661. /* functions called from platform device driver subsystem
  662. * need to have a relevant platform device entry in the platform's
  663. * board-*.c file
  664. */
  665. static const struct of_device_id kim_of_match[] = {
  666. {
  667. .compatible = "kim",
  668. },
  669. {}
  670. };
  671. MODULE_DEVICE_TABLE(of, kim_of_match);
  672. static struct ti_st_plat_data *get_platform_data(struct device *dev)
  673. {
  674. struct device_node *np = dev->of_node;
  675. const u32 *dt_property;
  676. int len;
  677. dt_pdata = kzalloc(sizeof(*dt_pdata), GFP_KERNEL);
  678. if (!dt_pdata)
  679. pr_err("Can't allocate device_tree platform data\n");
  680. dt_property = of_get_property(np, "dev_name", &len);
  681. if (dt_property)
  682. memcpy(&dt_pdata->dev_name, dt_property, len);
  683. of_property_read_u32(np, "nshutdown_gpio",
  684. &dt_pdata->nshutdown_gpio);
  685. of_property_read_u32(np, "flow_cntrl", &dt_pdata->flow_cntrl);
  686. of_property_read_u32(np, "baud_rate", &dt_pdata->baud_rate);
  687. return dt_pdata;
  688. }
  689. static struct dentry *kim_debugfs_dir;
  690. static int kim_probe(struct platform_device *pdev)
  691. {
  692. struct kim_data_s *kim_gdata;
  693. struct ti_st_plat_data *pdata;
  694. int err;
  695. if (pdev->dev.of_node)
  696. pdata = get_platform_data(&pdev->dev);
  697. else
  698. pdata = pdev->dev.platform_data;
  699. if (pdata == NULL) {
  700. dev_err(&pdev->dev, "Platform Data is missing\n");
  701. return -ENXIO;
  702. }
  703. if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
  704. /* multiple devices could exist */
  705. st_kim_devices[pdev->id] = pdev;
  706. } else {
  707. /* platform's sure about existence of 1 device */
  708. st_kim_devices[0] = pdev;
  709. }
  710. kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
  711. if (!kim_gdata) {
  712. pr_err("no mem to allocate");
  713. return -ENOMEM;
  714. }
  715. platform_set_drvdata(pdev, kim_gdata);
  716. err = st_core_init(&kim_gdata->core_data);
  717. if (err != 0) {
  718. pr_err(" ST core init failed");
  719. err = -EIO;
  720. goto err_core_init;
  721. }
  722. /* refer to itself */
  723. kim_gdata->core_data->kim_data = kim_gdata;
  724. /* Claim the chip enable nShutdown gpio from the system */
  725. kim_gdata->nshutdown = pdata->nshutdown_gpio;
  726. err = gpio_request(kim_gdata->nshutdown, "kim");
  727. if (unlikely(err)) {
  728. pr_err(" gpio %d request failed ", kim_gdata->nshutdown);
  729. return err;
  730. }
  731. /* Configure nShutdown GPIO as output=0 */
  732. err = gpio_direction_output(kim_gdata->nshutdown, 0);
  733. if (unlikely(err)) {
  734. pr_err(" unable to configure gpio %d", kim_gdata->nshutdown);
  735. return err;
  736. }
  737. /* get reference of pdev for request_firmware
  738. */
  739. kim_gdata->kim_pdev = pdev;
  740. init_completion(&kim_gdata->kim_rcvd);
  741. init_completion(&kim_gdata->ldisc_installed);
  742. err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
  743. if (err) {
  744. pr_err("failed to create sysfs entries");
  745. goto err_sysfs_group;
  746. }
  747. /* copying platform data */
  748. strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
  749. kim_gdata->flow_cntrl = pdata->flow_cntrl;
  750. kim_gdata->baud_rate = pdata->baud_rate;
  751. pr_info("sysfs entries created\n");
  752. kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
  753. if (!kim_debugfs_dir) {
  754. pr_err(" debugfs entries creation failed ");
  755. return 0;
  756. }
  757. debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
  758. kim_gdata, &version_debugfs_fops);
  759. debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
  760. kim_gdata, &list_debugfs_fops);
  761. return 0;
  762. err_sysfs_group:
  763. st_core_exit(kim_gdata->core_data);
  764. err_core_init:
  765. kfree(kim_gdata);
  766. return err;
  767. }
  768. static int kim_remove(struct platform_device *pdev)
  769. {
  770. /* free the GPIOs requested */
  771. struct ti_st_plat_data *pdata;
  772. struct kim_data_s *kim_gdata;
  773. if (pdev->dev.of_node) {
  774. pr_debug("use device tree data");
  775. pdata = dt_pdata;
  776. } else {
  777. pdata = pdev->dev.platform_data;
  778. }
  779. kim_gdata = platform_get_drvdata(pdev);
  780. /* Free the Bluetooth/FM/GPIO
  781. * nShutdown gpio from the system
  782. */
  783. gpio_free(pdata->nshutdown_gpio);
  784. pr_info("nshutdown GPIO Freed");
  785. debugfs_remove_recursive(kim_debugfs_dir);
  786. sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
  787. pr_info("sysfs entries removed");
  788. kim_gdata->kim_pdev = NULL;
  789. st_core_exit(kim_gdata->core_data);
  790. kfree(kim_gdata);
  791. kim_gdata = NULL;
  792. kfree(dt_pdata);
  793. dt_pdata = NULL;
  794. return 0;
  795. }
  796. static int kim_suspend(struct platform_device *pdev, pm_message_t state)
  797. {
  798. struct ti_st_plat_data *pdata;
  799. if (pdev->dev.of_node) {
  800. pr_debug("use device tree data");
  801. pdata = dt_pdata;
  802. } else {
  803. pdata = pdev->dev.platform_data;
  804. }
  805. if (pdata->suspend)
  806. return pdata->suspend(pdev, state);
  807. return 0;
  808. }
  809. static int kim_resume(struct platform_device *pdev)
  810. {
  811. struct ti_st_plat_data *pdata;
  812. if (pdev->dev.of_node) {
  813. pr_debug("use device tree data");
  814. pdata = dt_pdata;
  815. } else {
  816. pdata = pdev->dev.platform_data;
  817. }
  818. if (pdata->resume)
  819. return pdata->resume(pdev);
  820. return 0;
  821. }
  822. /**********************************************************************/
  823. /* entry point for ST KIM module, called in from ST Core */
  824. static struct platform_driver kim_platform_driver = {
  825. .probe = kim_probe,
  826. .remove = kim_remove,
  827. .suspend = kim_suspend,
  828. .resume = kim_resume,
  829. .driver = {
  830. .name = "kim",
  831. .owner = THIS_MODULE,
  832. .of_match_table = of_match_ptr(kim_of_match),
  833. },
  834. };
  835. module_platform_driver(kim_platform_driver);
  836. MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
  837. MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
  838. MODULE_LICENSE("GPL");