sipc4_modem.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* linux/drivers/misc/modem_if_v2/sipc4_modem.c
  2. *
  3. * Copyright (C) 2012 Samsung Electronics.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/fs.h>
  22. #include <linux/io.h>
  23. #include <linux/wait.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/mutex.h>
  27. #include <linux/irq.h>
  28. #include <linux/gpio.h>
  29. #include <linux/delay.h>
  30. #include <linux/wakelock.h>
  31. #include <linux/platform_data/modem.h>
  32. //#include <linux/platform_data/modem_v2.h>
  33. #include "modem_prj.h"
  34. #include "modem_variation.h"
  35. #include "modem_utils.h"
  36. /* If iod->id is 0, do not need to store to `iodevs_tree_fmt' in SIPC4 */
  37. #define sipc4_is_not_reserved_channel(ch) ((ch) != 0)
  38. static struct modem_shared *create_modem_shared_data(void)
  39. {
  40. struct modem_shared *msd;
  41. msd = kzalloc(sizeof(struct modem_shared), GFP_KERNEL);
  42. if (!msd)
  43. return NULL;
  44. /* initialize link device list */
  45. INIT_LIST_HEAD(&msd->link_dev_list);
  46. /* initialize tree of io devices */
  47. msd->iodevs_tree_chan = RB_ROOT;
  48. msd->iodevs_tree_fmt = RB_ROOT;
  49. return msd;
  50. }
  51. static struct modem_ctl *create_modemctl_device(struct platform_device *pdev,
  52. struct modem_shared *msd)
  53. {
  54. int ret = 0;
  55. struct modem_data *pdata;
  56. struct modem_ctl *modemctl;
  57. struct device *dev = &pdev->dev;
  58. /* create modem control device */
  59. modemctl = kzalloc(sizeof(struct modem_ctl), GFP_KERNEL);
  60. if (!modemctl)
  61. return NULL;
  62. modemctl->msd = msd;
  63. modemctl->dev = dev;
  64. modemctl->phone_state = STATE_OFFLINE;
  65. pdata = pdev->dev.platform_data;
  66. modemctl->mdm_data = pdata;
  67. modemctl->name = pdata->name;
  68. /* init modemctl device for getting modemctl operations */
  69. ret = call_modem_init_func(modemctl, pdata);
  70. if (ret) {
  71. kfree(modemctl);
  72. return NULL;
  73. }
  74. mif_info("%s: create_modemctl_device DONE\n", modemctl->name);
  75. return modemctl;
  76. }
  77. static struct io_device *create_io_device(struct modem_io_t *io_t,
  78. struct modem_shared *msd, struct modem_ctl *modemctl,
  79. struct modem_data *pdata)
  80. {
  81. int ret = 0;
  82. struct io_device *iod = NULL;
  83. iod = kzalloc(sizeof(struct io_device), GFP_KERNEL);
  84. if (!iod) {
  85. mif_err("io device memory alloc fail\n");
  86. return NULL;
  87. }
  88. rb_init_node(&iod->node_chan);
  89. rb_init_node(&iod->node_fmt);
  90. iod->name = io_t->name;
  91. iod->id = io_t->id;
  92. iod->format = io_t->format;
  93. iod->io_typ = io_t->io_type;
  94. iod->link_types = io_t->links;
  95. iod->net_typ = pdata->modem_net;
  96. iod->ipc_version = pdata->ipc_version;
  97. iod->use_handover = pdata->use_handover;
  98. atomic_set(&iod->opened, 0);
  99. /* link between io device and modem control */
  100. iod->mc = modemctl;
  101. if (iod->format == IPC_FMT)
  102. modemctl->iod = iod;
  103. else if (iod->format == IPC_BOOT)
  104. modemctl->bootd = iod;
  105. /* link between io device and modem shared */
  106. iod->msd = msd;
  107. /* add iod to rb_tree */
  108. if (iod->format != IPC_RAW)
  109. insert_iod_with_format(msd, iod->format, iod);
  110. if (sipc4_is_not_reserved_channel(iod->id))
  111. insert_iod_with_channel(msd, iod->id, iod);
  112. /* register misc device or net device */
  113. ret = sipc4_init_io_device(iod);
  114. if (ret) {
  115. kfree(iod);
  116. mif_err("sipc4_init_io_device fail (%d)\n", ret);
  117. return NULL;
  118. }
  119. mif_debug("%s: create_io_device DONE\n", io_t->name);
  120. return iod;
  121. }
  122. static int attach_devices(struct io_device *iod, enum modem_link tx_link)
  123. {
  124. struct modem_shared *msd = iod->msd;
  125. struct link_device *ld;
  126. /* find link type for this io device */
  127. list_for_each_entry(ld, &msd->link_dev_list, list) {
  128. if (IS_CONNECTED(iod, ld)) {
  129. /* The count 1 bits of iod->link_types is count
  130. * of link devices of this iod.
  131. * If use one link device,
  132. * or, 2+ link devices and this link is tx_link,
  133. * set iod's link device with ld
  134. */
  135. if ((countbits(iod->link_types) <= 1) ||
  136. (tx_link == ld->link_type)) {
  137. //mif_info("set %s->%s\n", iod->name, ld->name);
  138. set_current_link(iod, ld);
  139. if (iod->ipc_version == SIPC_VER_42) {
  140. if (iod->format == IPC_FMT) {
  141. int ch = iod->id & 0x03;
  142. mif_info("set %s->%s, ch[%d]\n", iod->name, ld->name, ch);
  143. ld->fmt_iods[ch] = iod;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. /* if use rx dynamic switch, set tx_link at modem_io_t of
  150. * board-*-modems.c
  151. */
  152. if (!get_current_link(iod)) {
  153. mif_err("%s->link == NULL\n", iod->name);
  154. BUG();
  155. }
  156. switch (iod->format) {
  157. case IPC_FMT:
  158. wake_lock_init(&iod->wakelock, WAKE_LOCK_SUSPEND, iod->name);
  159. iod->waketime = FMT_WAKE_TIME;
  160. break;
  161. case IPC_RFS:
  162. wake_lock_init(&iod->wakelock, WAKE_LOCK_SUSPEND, iod->name);
  163. iod->waketime = RFS_WAKE_TIME;
  164. break;
  165. case IPC_MULTI_RAW:
  166. wake_lock_init(&iod->wakelock, WAKE_LOCK_SUSPEND, iod->name);
  167. iod->waketime = RAW_WAKE_TIME;
  168. break;
  169. default:
  170. break;
  171. }
  172. return 0;
  173. }
  174. static int __devinit modem_probe(struct platform_device *pdev)
  175. {
  176. int i;
  177. struct modem_data *pdata = pdev->dev.platform_data;
  178. struct modem_shared *msd = NULL;
  179. struct modem_ctl *modemctl = NULL;
  180. struct io_device *iod[pdata->num_iodevs];
  181. struct link_device *ld;
  182. memset(iod, 0, sizeof(iod));
  183. msd = create_modem_shared_data();
  184. if (!msd) {
  185. mif_err("msd == NULL\n");
  186. goto err_free_modemctl;
  187. }
  188. modemctl = create_modemctl_device(pdev, msd);
  189. if (!modemctl) {
  190. mif_err("modemctl == NULL\n");
  191. goto err_free_modemctl;
  192. }
  193. /* create link device */
  194. /* support multi-link device */
  195. mif_err("call_link_init_func link type val [%d]\n", pdata->link_types);
  196. for (i = 0; i < LINKDEV_MAX ; i++) {
  197. /* find matching link type */
  198. if (pdata->link_types & LINKTYPE(i)) {
  199. mif_err("call_link_init_func [%d]\n", i);
  200. ld = call_link_init_func(pdev, i);
  201. if (!ld) {
  202. mif_err("call_link_init_func errori [%d]\n", i);
  203. goto err_free_modemctl;
  204. }
  205. mif_err("link created : %s\n", ld->name);
  206. ld->link_type = i;
  207. ld->mc = modemctl;
  208. ld->msd = msd;
  209. list_add(&ld->list, &msd->link_dev_list);
  210. }
  211. }
  212. /* create io deivces and connect to modemctl device */
  213. for (i = 0; i < pdata->num_iodevs; i++) {
  214. iod[i] = create_io_device(&pdata->iodevs[i], msd, modemctl,
  215. pdata);
  216. if (!iod[i]) {
  217. mif_err("iod[%d] == NULL\n", i);
  218. goto err_free_modemctl;
  219. }
  220. attach_devices(iod[i], pdata->iodevs[i].tx_link);
  221. }
  222. platform_set_drvdata(pdev, modemctl);
  223. mif_info("modem_probe Done\n");
  224. return 0;
  225. err_free_modemctl:
  226. for (i = 0; i < pdata->num_iodevs; i++)
  227. if (iod[i] != NULL)
  228. kfree(iod[i]);
  229. if (modemctl != NULL)
  230. kfree(modemctl);
  231. if (msd != NULL)
  232. kfree(msd);
  233. return -ENOMEM;
  234. }
  235. static void modem_shutdown(struct platform_device *pdev)
  236. {
  237. struct device *dev = &pdev->dev;
  238. struct modem_ctl *mc = dev_get_drvdata(dev);
  239. if (mc->ops.modem_off)
  240. mc->ops.modem_off(mc);
  241. mc->phone_state = STATE_OFFLINE;
  242. }
  243. static int modem_suspend(struct device *pdev)
  244. {
  245. struct modem_ctl *mc = dev_get_drvdata(pdev);
  246. if(mc->phone_state==STATE_OFFLINE)
  247. return 0;
  248. if (mc->gpio_pda_active)
  249. gpio_set_value(mc->gpio_pda_active, 0);
  250. return 0;
  251. }
  252. static int modem_resume(struct device *pdev)
  253. {
  254. struct modem_ctl *mc = dev_get_drvdata(pdev);
  255. if(mc->phone_state==STATE_OFFLINE)
  256. return 0;
  257. if (mc->gpio_pda_active)
  258. gpio_set_value(mc->gpio_pda_active, 1);
  259. return 0;
  260. }
  261. static const struct dev_pm_ops modem_pm_ops = {
  262. .suspend = modem_suspend,
  263. .resume = modem_resume,
  264. };
  265. #ifdef CONFIG_OF
  266. static const struct of_device_id gsm_modem_dt_match[] = {
  267. {.compatible = "samsung,sc6500_modem"},
  268. {}
  269. };
  270. MODULE_DEVICE_TABLE(of, gsm_modem_dt_match);
  271. #endif
  272. static struct platform_driver modem_driver = {
  273. .probe = modem_probe,
  274. .shutdown = modem_shutdown,
  275. .driver = {
  276. .name = "mif_sipc4",
  277. .pm = &modem_pm_ops,
  278. #ifdef CONFIG_OF
  279. .of_match_table = gsm_modem_dt_match,
  280. #endif
  281. },
  282. };
  283. static int __init modem_init(void)
  284. {
  285. return platform_driver_register(&modem_driver);
  286. }
  287. module_init(modem_init);
  288. MODULE_LICENSE("GPL");
  289. MODULE_DESCRIPTION("Samsung Modem Interface Driver");