st_kim.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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/rfkill.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/ti_wilink_st.h>
  35. static int kim_probe(struct platform_device *pdev);
  36. static int kim_remove(struct platform_device *pdev);
  37. /* KIM platform device driver structure */
  38. static struct platform_driver kim_platform_driver = {
  39. .probe = kim_probe,
  40. .remove = kim_remove,
  41. /* TODO: ST driver power management during suspend/resume ?
  42. */
  43. #if 0
  44. .suspend = kim_suspend,
  45. .resume = kim_resume,
  46. #endif
  47. .driver = {
  48. .name = "kim",
  49. .owner = THIS_MODULE,
  50. },
  51. };
  52. static int kim_toggle_radio(void*, bool);
  53. static const struct rfkill_ops kim_rfkill_ops = {
  54. .set_block = kim_toggle_radio,
  55. };
  56. /* strings to be used for rfkill entries and by
  57. * ST Core to be used for sysfs debug entry
  58. */
  59. #define PROTO_ENTRY(type, name) name
  60. const unsigned char *protocol_names[] = {
  61. PROTO_ENTRY(ST_BT, "Bluetooth"),
  62. PROTO_ENTRY(ST_FM, "FM"),
  63. PROTO_ENTRY(ST_GPS, "GPS"),
  64. };
  65. #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
  66. static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
  67. /**********************************************************************/
  68. /* internal functions */
  69. /**
  70. * st_get_plat_device -
  71. * function which returns the reference to the platform device
  72. * requested by id. As of now only 1 such device exists (id=0)
  73. * the context requesting for reference can get the id to be
  74. * requested by a. The protocol driver which is registering or
  75. * b. the tty device which is opened.
  76. */
  77. static struct platform_device *st_get_plat_device(int id)
  78. {
  79. return st_kim_devices[id];
  80. }
  81. /**
  82. * validate_firmware_response -
  83. * function to return whether the firmware response was proper
  84. * in case of error don't complete so that waiting for proper
  85. * response times out
  86. */
  87. void validate_firmware_response(struct kim_data_s *kim_gdata)
  88. {
  89. struct sk_buff *skb = kim_gdata->rx_skb;
  90. if (unlikely(skb->data[5] != 0)) {
  91. pr_err("no proper response during fw download");
  92. pr_err("data6 %x", skb->data[5]);
  93. return; /* keep waiting for the proper response */
  94. }
  95. /* becos of all the script being downloaded */
  96. complete_all(&kim_gdata->kim_rcvd);
  97. kfree_skb(skb);
  98. }
  99. /* check for data len received inside kim_int_recv
  100. * most often hit the last case to update state to waiting for data
  101. */
  102. static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
  103. {
  104. register int room = skb_tailroom(kim_gdata->rx_skb);
  105. pr_debug("len %d room %d", len, room);
  106. if (!len) {
  107. validate_firmware_response(kim_gdata);
  108. } else if (len > room) {
  109. /* Received packet's payload length is larger.
  110. * We can't accommodate it in created skb.
  111. */
  112. pr_err("Data length is too large len %d room %d", len,
  113. room);
  114. kfree_skb(kim_gdata->rx_skb);
  115. } else {
  116. /* Packet header has non-zero payload length and
  117. * we have enough space in created skb. Lets read
  118. * payload data */
  119. kim_gdata->rx_state = ST_W4_DATA;
  120. kim_gdata->rx_count = len;
  121. return len;
  122. }
  123. /* Change ST LL state to continue to process next
  124. * packet */
  125. kim_gdata->rx_state = ST_W4_PACKET_TYPE;
  126. kim_gdata->rx_skb = NULL;
  127. kim_gdata->rx_count = 0;
  128. return 0;
  129. }
  130. /**
  131. * kim_int_recv - receive function called during firmware download
  132. * firmware download responses on different UART drivers
  133. * have been observed to come in bursts of different
  134. * tty_receive and hence the logic
  135. */
  136. void kim_int_recv(struct kim_data_s *kim_gdata,
  137. const unsigned char *data, long count)
  138. {
  139. const unsigned char *ptr;
  140. int len = 0, type = 0;
  141. unsigned char *plen;
  142. pr_debug("%s", __func__);
  143. /* Decode received bytes here */
  144. ptr = data;
  145. if (unlikely(ptr == NULL)) {
  146. pr_err(" received null from TTY ");
  147. return;
  148. }
  149. while (count) {
  150. if (kim_gdata->rx_count) {
  151. len = min_t(unsigned int, kim_gdata->rx_count, count);
  152. memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
  153. kim_gdata->rx_count -= len;
  154. count -= len;
  155. ptr += len;
  156. if (kim_gdata->rx_count)
  157. continue;
  158. /* Check ST RX state machine , where are we? */
  159. switch (kim_gdata->rx_state) {
  160. /* Waiting for complete packet ? */
  161. case ST_W4_DATA:
  162. pr_debug("Complete pkt received");
  163. validate_firmware_response(kim_gdata);
  164. kim_gdata->rx_state = ST_W4_PACKET_TYPE;
  165. kim_gdata->rx_skb = NULL;
  166. continue;
  167. /* Waiting for Bluetooth event header ? */
  168. case ST_W4_HEADER:
  169. plen =
  170. (unsigned char *)&kim_gdata->rx_skb->data[1];
  171. pr_debug("event hdr: plen 0x%02x\n", *plen);
  172. kim_check_data_len(kim_gdata, *plen);
  173. continue;
  174. } /* end of switch */
  175. } /* end of if rx_state */
  176. switch (*ptr) {
  177. /* Bluetooth event packet? */
  178. case 0x04:
  179. kim_gdata->rx_state = ST_W4_HEADER;
  180. kim_gdata->rx_count = 2;
  181. type = *ptr;
  182. break;
  183. default:
  184. pr_info("unknown packet");
  185. ptr++;
  186. count--;
  187. continue;
  188. }
  189. ptr++;
  190. count--;
  191. kim_gdata->rx_skb =
  192. alloc_skb(1024+8, GFP_ATOMIC);
  193. if (!kim_gdata->rx_skb) {
  194. pr_err("can't allocate mem for new packet");
  195. kim_gdata->rx_state = ST_W4_PACKET_TYPE;
  196. kim_gdata->rx_count = 0;
  197. return;
  198. }
  199. skb_reserve(kim_gdata->rx_skb, 8);
  200. kim_gdata->rx_skb->cb[0] = 4;
  201. kim_gdata->rx_skb->cb[1] = 0;
  202. }
  203. return;
  204. }
  205. static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
  206. {
  207. unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
  208. const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
  209. pr_debug("%s", __func__);
  210. INIT_COMPLETION(kim_gdata->kim_rcvd);
  211. if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
  212. pr_err("kim: couldn't write 4 bytes");
  213. return -1;
  214. }
  215. if (!wait_for_completion_timeout
  216. (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
  217. pr_err(" waiting for ver info- timed out ");
  218. return -1;
  219. }
  220. version =
  221. MAKEWORD(kim_gdata->resp_buffer[13],
  222. kim_gdata->resp_buffer[14]);
  223. chip = (version & 0x7C00) >> 10;
  224. min_ver = (version & 0x007F);
  225. maj_ver = (version & 0x0380) >> 7;
  226. if (version & 0x8000)
  227. maj_ver |= 0x0008;
  228. sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", 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. /**
  238. * download_firmware -
  239. * internal function which parses through the .bts firmware
  240. * script file intreprets SEND, DELAY actions only as of now
  241. */
  242. static long download_firmware(struct kim_data_s *kim_gdata)
  243. {
  244. long err = 0;
  245. long len = 0;
  246. unsigned char *ptr = NULL;
  247. unsigned char *action_ptr = NULL;
  248. unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
  249. err = read_local_version(kim_gdata, bts_scr_name);
  250. if (err != 0) {
  251. pr_err("kim: failed to read local ver");
  252. return err;
  253. }
  254. err =
  255. request_firmware(&kim_gdata->fw_entry, bts_scr_name,
  256. &kim_gdata->kim_pdev->dev);
  257. if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
  258. (kim_gdata->fw_entry->size == 0))) {
  259. pr_err(" request_firmware failed(errno %ld) for %s", err,
  260. bts_scr_name);
  261. return -1;
  262. }
  263. ptr = (void *)kim_gdata->fw_entry->data;
  264. len = kim_gdata->fw_entry->size;
  265. /* bts_header to remove out magic number and
  266. * version
  267. */
  268. ptr += sizeof(struct bts_header);
  269. len -= sizeof(struct bts_header);
  270. while (len > 0 && ptr) {
  271. pr_debug(" action size %d, type %d ",
  272. ((struct bts_action *)ptr)->size,
  273. ((struct bts_action *)ptr)->type);
  274. switch (((struct bts_action *)ptr)->type) {
  275. case ACTION_SEND_COMMAND: /* action send */
  276. action_ptr = &(((struct bts_action *)ptr)->data[0]);
  277. if (unlikely
  278. (((struct hci_command *)action_ptr)->opcode ==
  279. 0xFF36)) {
  280. /* ignore remote change
  281. * baud rate HCI VS command */
  282. pr_err
  283. (" change remote baud"
  284. " rate command in firmware");
  285. break;
  286. }
  287. INIT_COMPLETION(kim_gdata->kim_rcvd);
  288. err = st_int_write(kim_gdata->core_data,
  289. ((struct bts_action_send *)action_ptr)->data,
  290. ((struct bts_action *)ptr)->size);
  291. if (unlikely(err < 0)) {
  292. release_firmware(kim_gdata->fw_entry);
  293. return -1;
  294. }
  295. if (!wait_for_completion_timeout
  296. (&kim_gdata->kim_rcvd,
  297. msecs_to_jiffies(CMD_RESP_TIME))) {
  298. pr_err
  299. (" response timeout during fw download ");
  300. /* timed out */
  301. release_firmware(kim_gdata->fw_entry);
  302. return -1;
  303. }
  304. break;
  305. case ACTION_DELAY: /* sleep */
  306. pr_info("sleep command in scr");
  307. action_ptr = &(((struct bts_action *)ptr)->data[0]);
  308. mdelay(((struct bts_action_delay *)action_ptr)->msec);
  309. break;
  310. }
  311. len =
  312. len - (sizeof(struct bts_action) +
  313. ((struct bts_action *)ptr)->size);
  314. ptr =
  315. ptr + sizeof(struct bts_action) +
  316. ((struct bts_action *)ptr)->size;
  317. }
  318. /* fw download complete */
  319. release_firmware(kim_gdata->fw_entry);
  320. return 0;
  321. }
  322. /**********************************************************************/
  323. /* functions called from ST core */
  324. /* function to toggle the GPIO
  325. * needs to know whether the GPIO is active high or active low
  326. */
  327. void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
  328. {
  329. struct platform_device *kim_pdev;
  330. struct kim_data_s *kim_gdata;
  331. pr_info(" %s ", __func__);
  332. kim_pdev = st_get_plat_device(0);
  333. kim_gdata = dev_get_drvdata(&kim_pdev->dev);
  334. if (kim_gdata->gpios[type] == -1) {
  335. pr_info(" gpio not requested for protocol %s",
  336. protocol_names[type]);
  337. return;
  338. }
  339. switch (type) {
  340. case ST_BT:
  341. /*Do Nothing */
  342. break;
  343. case ST_FM:
  344. if (state == KIM_GPIO_ACTIVE)
  345. gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
  346. else
  347. gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
  348. break;
  349. case ST_GPS:
  350. if (state == KIM_GPIO_ACTIVE)
  351. gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
  352. else
  353. gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
  354. break;
  355. case ST_MAX_CHANNELS:
  356. default:
  357. break;
  358. }
  359. return;
  360. }
  361. /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
  362. * can be because of
  363. * 1. response to read local version
  364. * 2. during send/recv's of firmware download
  365. */
  366. void st_kim_recv(void *disc_data, const unsigned char *data, long count)
  367. {
  368. struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
  369. struct kim_data_s *kim_gdata = st_gdata->kim_data;
  370. /* copy to local buffer */
  371. if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
  372. /* must be the read_ver_cmd */
  373. memcpy(kim_gdata->resp_buffer, data, count);
  374. complete_all(&kim_gdata->kim_rcvd);
  375. return;
  376. } else {
  377. kim_int_recv(kim_gdata, data, count);
  378. /* either completes or times out */
  379. }
  380. return;
  381. }
  382. /* to signal completion of line discipline installation
  383. * called from ST Core, upon tty_open
  384. */
  385. void st_kim_complete(void *kim_data)
  386. {
  387. struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
  388. complete(&kim_gdata->ldisc_installed);
  389. }
  390. /**
  391. * st_kim_start - called from ST Core upon 1st registration
  392. * This involves toggling the chip enable gpio, reading
  393. * the firmware version from chip, forming the fw file name
  394. * based on the chip version, requesting the fw, parsing it
  395. * and perform download(send/recv).
  396. */
  397. long st_kim_start(void *kim_data)
  398. {
  399. long err = 0;
  400. long retry = POR_RETRY_COUNT;
  401. struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
  402. pr_info(" %s", __func__);
  403. do {
  404. /* TODO: this is only because rfkill sub-system
  405. * doesn't send events to user-space if the state
  406. * isn't changed
  407. */
  408. rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
  409. /* Configure BT nShutdown to HIGH state */
  410. gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
  411. mdelay(5); /* FIXME: a proper toggle */
  412. gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
  413. mdelay(100);
  414. /* re-initialize the completion */
  415. INIT_COMPLETION(kim_gdata->ldisc_installed);
  416. #if 0 /* older way of signalling user-space UIM */
  417. /* send signal to UIM */
  418. err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
  419. if (err != 0) {
  420. pr_info(" sending SIGUSR2 to uim failed %ld", err);
  421. err = -1;
  422. continue;
  423. }
  424. #endif
  425. /* unblock and send event to UIM via /dev/rfkill */
  426. rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
  427. /* wait for ldisc to be installed */
  428. err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
  429. msecs_to_jiffies(LDISC_TIME));
  430. if (!err) { /* timeout */
  431. pr_err("line disc installation timed out ");
  432. err = -1;
  433. continue;
  434. } else {
  435. /* ldisc installed now */
  436. pr_info(" line discipline installed ");
  437. err = download_firmware(kim_gdata);
  438. if (err != 0) {
  439. pr_err("download firmware failed");
  440. continue;
  441. } else { /* on success don't retry */
  442. break;
  443. }
  444. }
  445. } while (retry--);
  446. return err;
  447. }
  448. /**
  449. * st_kim_stop - called from ST Core, on the last un-registration
  450. * toggle low the chip enable gpio
  451. */
  452. long st_kim_stop(void *kim_data)
  453. {
  454. long err = 0;
  455. struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
  456. INIT_COMPLETION(kim_gdata->ldisc_installed);
  457. #if 0 /* older way of signalling user-space UIM */
  458. /* send signal to UIM */
  459. err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
  460. if (err != 0) {
  461. pr_err("sending SIGUSR2 to uim failed %ld", err);
  462. return -1;
  463. }
  464. #endif
  465. /* set BT rfkill to be blocked */
  466. err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
  467. /* wait for ldisc to be un-installed */
  468. err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
  469. msecs_to_jiffies(LDISC_TIME));
  470. if (!err) { /* timeout */
  471. pr_err(" timed out waiting for ldisc to be un-installed");
  472. return -1;
  473. }
  474. /* By default configure BT nShutdown to LOW state */
  475. gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
  476. mdelay(1);
  477. gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
  478. mdelay(1);
  479. gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
  480. return err;
  481. }
  482. /**********************************************************************/
  483. /* functions called from subsystems */
  484. /* called when debugfs entry is read from */
  485. static int show_version(struct seq_file *s, void *unused)
  486. {
  487. struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
  488. seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
  489. kim_gdata->version.chip, kim_gdata->version.maj_ver,
  490. kim_gdata->version.min_ver);
  491. return 0;
  492. }
  493. static int show_list(struct seq_file *s, void *unused)
  494. {
  495. struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
  496. kim_st_list_protocols(kim_gdata->core_data, s);
  497. return 0;
  498. }
  499. /* function called from rfkill subsystem, when someone from
  500. * user space would write 0/1 on the sysfs entry
  501. * /sys/class/rfkill/rfkill0,1,3/state
  502. */
  503. static int kim_toggle_radio(void *data, bool blocked)
  504. {
  505. enum proto_type type = *((enum proto_type *)data);
  506. pr_debug(" %s: %d ", __func__, type);
  507. switch (type) {
  508. case ST_BT:
  509. /* do nothing */
  510. break;
  511. case ST_FM:
  512. case ST_GPS:
  513. if (blocked)
  514. st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
  515. else
  516. st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
  517. break;
  518. case ST_MAX_CHANNELS:
  519. pr_err(" wrong proto type ");
  520. break;
  521. }
  522. return 0;
  523. }
  524. /**
  525. * st_kim_ref - reference the core's data
  526. * This references the per-ST platform device in the arch/xx/
  527. * board-xx.c file.
  528. * This would enable multiple such platform devices to exist
  529. * on a given platform
  530. */
  531. void st_kim_ref(struct st_data_s **core_data, int id)
  532. {
  533. struct platform_device *pdev;
  534. struct kim_data_s *kim_gdata;
  535. /* get kim_gdata reference from platform device */
  536. pdev = st_get_plat_device(id);
  537. kim_gdata = dev_get_drvdata(&pdev->dev);
  538. *core_data = kim_gdata->core_data;
  539. }
  540. static int kim_version_open(struct inode *i, struct file *f)
  541. {
  542. return single_open(f, show_version, i->i_private);
  543. }
  544. static int kim_list_open(struct inode *i, struct file *f)
  545. {
  546. return single_open(f, show_list, i->i_private);
  547. }
  548. static const struct file_operations version_debugfs_fops = {
  549. /* version info */
  550. .open = kim_version_open,
  551. .read = seq_read,
  552. .llseek = seq_lseek,
  553. .release = single_release,
  554. };
  555. static const struct file_operations list_debugfs_fops = {
  556. /* protocols info */
  557. .open = kim_list_open,
  558. .read = seq_read,
  559. .llseek = seq_lseek,
  560. .release = single_release,
  561. };
  562. /**********************************************************************/
  563. /* functions called from platform device driver subsystem
  564. * need to have a relevant platform device entry in the platform's
  565. * board-*.c file
  566. */
  567. struct dentry *kim_debugfs_dir;
  568. static int kim_probe(struct platform_device *pdev)
  569. {
  570. long status;
  571. long proto;
  572. long *gpios = pdev->dev.platform_data;
  573. struct kim_data_s *kim_gdata;
  574. if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
  575. /* multiple devices could exist */
  576. st_kim_devices[pdev->id] = pdev;
  577. } else {
  578. /* platform's sure about existance of 1 device */
  579. st_kim_devices[0] = pdev;
  580. }
  581. kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
  582. if (!kim_gdata) {
  583. pr_err("no mem to allocate");
  584. return -ENOMEM;
  585. }
  586. dev_set_drvdata(&pdev->dev, kim_gdata);
  587. status = st_core_init(&kim_gdata->core_data);
  588. if (status != 0) {
  589. pr_err(" ST core init failed");
  590. return -1;
  591. }
  592. /* refer to itself */
  593. kim_gdata->core_data->kim_data = kim_gdata;
  594. for (proto = 0; proto < ST_MAX_CHANNELS; proto++) {
  595. kim_gdata->gpios[proto] = gpios[proto];
  596. pr_info(" %ld gpio to be requested", gpios[proto]);
  597. }
  598. for (proto = 0; (proto < ST_MAX_CHANNELS)
  599. && (gpios[proto] != -1); proto++) {
  600. /* Claim the Bluetooth/FM/GPIO
  601. * nShutdown gpio from the system
  602. */
  603. status = gpio_request(gpios[proto], "kim");
  604. if (unlikely(status)) {
  605. pr_err(" gpio %ld request failed ", gpios[proto]);
  606. proto -= 1;
  607. while (proto >= 0) {
  608. if (gpios[proto] != -1)
  609. gpio_free(gpios[proto]);
  610. }
  611. return status;
  612. }
  613. /* Configure nShutdown GPIO as output=0 */
  614. status =
  615. gpio_direction_output(gpios[proto], 0);
  616. if (unlikely(status)) {
  617. pr_err(" unable to configure gpio %ld",
  618. gpios[proto]);
  619. proto -= 1;
  620. while (proto >= 0) {
  621. if (gpios[proto] != -1)
  622. gpio_free(gpios[proto]);
  623. }
  624. return status;
  625. }
  626. }
  627. /* get reference of pdev for request_firmware
  628. */
  629. kim_gdata->kim_pdev = pdev;
  630. init_completion(&kim_gdata->kim_rcvd);
  631. init_completion(&kim_gdata->ldisc_installed);
  632. for (proto = 0; (proto < ST_MAX_CHANNELS)
  633. && (gpios[proto] != -1); proto++) {
  634. /* TODO: should all types be rfkill_type_bt ? */
  635. kim_gdata->rf_protos[proto] = proto;
  636. kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
  637. &pdev->dev, RFKILL_TYPE_BLUETOOTH,
  638. &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
  639. if (kim_gdata->rfkill[proto] == NULL) {
  640. pr_err("cannot create rfkill entry for gpio %ld",
  641. gpios[proto]);
  642. continue;
  643. }
  644. /* block upon creation */
  645. rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
  646. status = rfkill_register(kim_gdata->rfkill[proto]);
  647. if (unlikely(status)) {
  648. pr_err("rfkill registration failed for gpio %ld",
  649. gpios[proto]);
  650. rfkill_unregister(kim_gdata->rfkill[proto]);
  651. continue;
  652. }
  653. pr_info("rfkill entry created for %ld", gpios[proto]);
  654. }
  655. kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
  656. if (IS_ERR(kim_debugfs_dir)) {
  657. pr_err(" debugfs entries creation failed ");
  658. kim_debugfs_dir = NULL;
  659. return -1;
  660. }
  661. debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
  662. kim_gdata, &version_debugfs_fops);
  663. debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
  664. kim_gdata, &list_debugfs_fops);
  665. pr_info(" debugfs entries created ");
  666. return 0;
  667. }
  668. static int kim_remove(struct platform_device *pdev)
  669. {
  670. /* free the GPIOs requested
  671. */
  672. long *gpios = pdev->dev.platform_data;
  673. long proto;
  674. struct kim_data_s *kim_gdata;
  675. kim_gdata = dev_get_drvdata(&pdev->dev);
  676. for (proto = 0; (proto < ST_MAX_CHANNELS)
  677. && (gpios[proto] != -1); proto++) {
  678. /* Claim the Bluetooth/FM/GPIO
  679. * nShutdown gpio from the system
  680. */
  681. gpio_free(gpios[proto]);
  682. rfkill_unregister(kim_gdata->rfkill[proto]);
  683. rfkill_destroy(kim_gdata->rfkill[proto]);
  684. kim_gdata->rfkill[proto] = NULL;
  685. }
  686. pr_info("kim: GPIO Freed");
  687. debugfs_remove_recursive(kim_debugfs_dir);
  688. kim_gdata->kim_pdev = NULL;
  689. st_core_exit(kim_gdata->core_data);
  690. kfree(kim_gdata);
  691. kim_gdata = NULL;
  692. return 0;
  693. }
  694. /**********************************************************************/
  695. /* entry point for ST KIM module, called in from ST Core */
  696. static int __init st_kim_init(void)
  697. {
  698. long ret = 0;
  699. ret = platform_driver_register(&kim_platform_driver);
  700. if (ret != 0) {
  701. pr_err("platform drv registration failed");
  702. return -1;
  703. }
  704. return 0;
  705. }
  706. static void __exit st_kim_deinit(void)
  707. {
  708. /* the following returns void */
  709. platform_driver_unregister(&kim_platform_driver);
  710. }
  711. module_init(st_kim_init);
  712. module_exit(st_kim_deinit);
  713. MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
  714. MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
  715. MODULE_LICENSE("GPL");