gps.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*
  2. * drivers/barcelona/gps/gps.c
  3. *
  4. * Implementation of the GPS driver.
  5. *
  6. * Copyright (C) 2004,2005 TomTom BV <http://www.tomtom.com/>
  7. * Authors:
  8. * Jeroen Taverne <jeroen.taverne@tomtom.com>
  9. * Dimitry Andric <dimitry.andric@tomtom.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. /*******************************************************************************
  16. * Dependency
  17. *******************************************************************************/
  18. #ifdef CONFIG_MTK_GPS_SUPPORT
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/wait.h>
  24. #include <linux/slab.h>
  25. #include <linux/fs.h>
  26. #include <linux/sched.h>
  27. #include <linux/poll.h>
  28. #include <linux/device.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/cdev.h>
  33. #include <linux/errno.h>
  34. #include <asm/io.h>
  35. #include <asm/uaccess.h>
  36. /* #include <linux/xlog.h> */
  37. #include <linux/printk.h>
  38. #include <linux/semaphore.h>
  39. #include <linux/version.h>
  40. #ifdef pr_fmt
  41. #undef pr_fmt
  42. #endif
  43. #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
  44. /******************************************************************************
  45. * Function Configuration
  46. ******************************************************************************/
  47. /* #define FAKE_DATA */
  48. #define GPS_SUSPEND_RESUME
  49. #define GPS_CONFIGURABLE_RESET_DELAY
  50. /******************************************************************************
  51. * Definition
  52. ******************************************************************************/
  53. /* device name and major number */
  54. #define GPS_DEVNAME "mt3326-gps"
  55. /******************************************************************************
  56. * Debug configuration
  57. ******************************************************************************/
  58. #if 0
  59. #define GPS_DBG_FUNC(fmt, arg...) pr_debug(PFX, "%s: " fmt, __func__ , ##arg)
  60. #define GPS_ERR(fmt, arg...) pr_err(PFX, "%s: " fmt, __func__ , ##arg)
  61. #define GPS_WARN(fmt, arg...) pr_warn(PFX, "%s" fmt, __func__ , ##arg)
  62. #define GPS_NOTICE(fmt, arg...) pr_notice(PFX, "%s: " fmt, __func__ , ##arg)
  63. #define GPS_INFO(fmt, arg...) pr_info(PFX, "%s: " fmt, __func__ , ##arg)
  64. #define PFX "GPS: "
  65. #else
  66. #define GPS_DBG_NONE(fmt, arg...) do {} while (0)
  67. #define GPS_DBG pr_debug
  68. #define GPS_TRC GPS_DBG_NONE
  69. #define GPS_VER pr_debug
  70. #define GPS_ERR pr_err
  71. #endif
  72. /*******************************************************************************
  73. * structure & enumeration
  74. *******************************************************************************/
  75. enum {
  76. GPS_PWRCTL_UNSUPPORTED = 0xFF,
  77. GPS_PWRCTL_OFF = 0x00,
  78. GPS_PWRCTL_ON = 0x01,
  79. GPS_PWRCTL_RST = 0x02,
  80. GPS_PWRCTL_OFF_FORCE = 0x03,
  81. GPS_PWRCTL_RST_FORCE = 0x04,
  82. GPS_PWRCTL_MAX = 0x05,
  83. };
  84. enum {
  85. GPS_PWR_UNSUPPORTED = 0xFF,
  86. GPS_PWR_RESUME = 0x00,
  87. GPS_PWR_SUSPEND = 0x01,
  88. GPS_PWR_MAX = 0x02,
  89. };
  90. enum {
  91. GPS_STATE_UNSUPPORTED = 0xFF,
  92. GPS_STATE_OFF = 0x00, /*cleanup/power off, default state */
  93. GPS_STATE_INIT = 0x01, /*init */
  94. GPS_STATE_START = 0x02, /*start navigating */
  95. GPS_STATE_STOP = 0x03, /*stop navigating */
  96. GPS_STATE_DEC_FREQ = 0x04,
  97. GPS_STATE_SLEEP = 0x05,
  98. GPS_STATE_MAX = 0x06,
  99. };
  100. enum {
  101. GPS_PWRSAVE_UNSUPPORTED = 0xFF,
  102. GPS_PWRSAVE_DEC_FREQ = 0x00,
  103. GPS_PWRSAVE_SLEEP = 0x01,
  104. GPS_PWRSAVE_OFF = 0x02,
  105. GPS_PWRSAVE_MAX = 0x03,
  106. };
  107. /*---------------------------------------------------------------------------*/
  108. struct gps_data {
  109. int dat_len;
  110. int dat_pos;
  111. char dat_buf[4096];
  112. spinlock_t lock;
  113. wait_queue_head_t read_wait;
  114. struct semaphore sem;
  115. };
  116. /*---------------------------------------------------------------------------*/
  117. struct gps_sta_itm { /*gps status record */
  118. unsigned char year; /*current year - 1900 */
  119. unsigned char month; /*1~12 */
  120. unsigned char day; /*1~31 */
  121. unsigned char hour; /*0~23 */
  122. unsigned char minute; /*0~59 */
  123. unsigned char sec; /*0~59 */
  124. unsigned char count; /*reborn count */
  125. unsigned char reason; /*reason: 0: timeout; 1: force */
  126. };
  127. /*---------------------------------------------------------------------------*/
  128. struct gps_sta_obj {
  129. int index;
  130. struct gps_sta_itm items[32];
  131. };
  132. /*---------------------------------------------------------------------------*/
  133. struct gps_drv_obj {
  134. unsigned char pwrctl;
  135. unsigned char suspend;
  136. unsigned char state;
  137. unsigned char pwrsave;
  138. int rdelay; /*power reset delay */
  139. struct kobject *kobj;
  140. struct mutex sem;
  141. struct gps_sta_obj status;
  142. struct mt3326_gps_hardware *hw;
  143. };
  144. /*---------------------------------------------------------------------------*/
  145. struct gps_dev_obj {
  146. struct class *cls;
  147. struct device *dev;
  148. dev_t devno;
  149. struct cdev chdev;
  150. struct mt3326_gps_hardware *hw;
  151. };
  152. /******************************************************************************
  153. * GPS driver
  154. ******************************************************************************/
  155. struct mt3326_gps_hardware {
  156. int (*ext_power_on)(int);
  157. int (*ext_power_off)(int);
  158. };
  159. struct mt3326_gps_hardware mt3326_gps_hw = {
  160. .ext_power_on = NULL,
  161. .ext_power_off = NULL,
  162. };
  163. /******************************************************************************
  164. * local variables
  165. ******************************************************************************/
  166. static struct gps_data gps_private = { 0 };
  167. #if defined(FAKE_DATA)
  168. static char fake_data[] = {
  169. "$GPGGA,135036.000,2446.3713,N,12101.3605,E,1,5,1.61,191.1,M,15.1,M,,*51\r\n"
  170. "$GPGSA,A,3,22,18,14,30,31,,,,,,,,1.88,1.61,0.98*09\r\n"
  171. "$GPGSV,2,1,6,18,83,106,32,22,58,324,35,30,45,157,35,14,28,308,32*44\r\n"
  172. "$GPGSV,2,2,6,40,21,254,,31,17,237,29*42\r\n"
  173. "$GPRMC,135036.000,A,2446.37125,N,12101.36054,E,0.243,56.48,140109,,A*46\r\n"
  174. "$GPVTG,56.48,T,,M,0.243,N,0.451,K,A*07\r\n"
  175. };
  176. #endif /* FAKE_DATA */
  177. /*this should be synchronous with mnld.c
  178. enum {
  179. MNL_RESTART_NONE = 0x00, //recording the 1st of mnld
  180. MNL_RESTART_TIMEOUT_INIT = 0x01, //restart due to timeout
  181. MNL_RESTART_TIMEOUT_MONITOR = 0x02, //restart due to timeout
  182. MNL_RESTART_TIMEOUT_WAKEUP = 0x03, //restart due to timeout
  183. MNL_RESTART_TIMEOUT_TTFF = 0x04, //restart due to TTFF timeout
  184. MNL_RESTART_FORCE = 0x04, //restart due to external command
  185. };
  186. */
  187. /*---------------------------------------------------------------------------*/
  188. static char *str_reason[] = { "none", "init", "monitor", "wakeup", "TTFF", "force", "unknown" };
  189. /******************************************************************************
  190. * Functions
  191. ******************************************************************************/
  192. static inline void mt3326_gps_power(struct mt3326_gps_hardware *hw, unsigned int on, unsigned int force)
  193. {
  194. /*FIX ME: PM_api should provide a function to get current status */
  195. static unsigned int power_on = 1;
  196. int err;
  197. GPS_DBG("Switching GPS device %s\n", on ? "on" : "off");
  198. if (!hw) {
  199. GPS_ERR("null pointer!!\n");
  200. return;
  201. }
  202. if (power_on == on) {
  203. GPS_DBG("ignore power control: %d\n", on);
  204. } else if (on) {
  205. /*power on */
  206. if (hw->ext_power_on) {
  207. err = hw->ext_power_on(0);
  208. if (err)
  209. GPS_ERR("ext_power_on fail\n");
  210. }
  211. #if 0
  212. #ifndef MTK_MT6620
  213. if (!hwPowerOn(MT6516_POWER_V3GTX, VOL_2800, "MT3326")) {
  214. GPS_ERR("power on fails!!\n");
  215. return;
  216. }
  217. #endif
  218. #endif
  219. if (hw->ext_power_on) {
  220. err = hw->ext_power_on(1);
  221. if (err)
  222. GPS_ERR("ext_power_on fail\n");
  223. }
  224. #if 0
  225. if (!hwEnableClock(MT6516_PDN_PERI_UART2, "MT3326"))
  226. GPS_ERR("enable clock fail");
  227. #endif
  228. mdelay(120);
  229. } else {
  230. #if 0
  231. if (!hwDisableClock(MT6516_PDN_PERI_UART2, "MT3326"))
  232. GPS_ERR("disable clock fail\n");
  233. #endif
  234. if (hw->ext_power_off) {
  235. err = hw->ext_power_off(force);
  236. if (err)
  237. GPS_ERR("ext_power_off fail\n");
  238. }
  239. }
  240. power_on = on;
  241. }
  242. /*****************************************************************************/
  243. static inline void mt3326_gps_reset(struct mt3326_gps_hardware *hw, int delay, int force)
  244. {
  245. mt3326_gps_power(hw, 1, 0);
  246. mdelay(delay);
  247. mt3326_gps_power(hw, 0, force);
  248. mdelay(delay);
  249. mt3326_gps_power(hw, 1, 0);
  250. }
  251. /******************************************************************************/
  252. static inline int mt3326_gps_set_suspend(struct gps_drv_obj *obj, unsigned char suspend)
  253. {
  254. if (!obj)
  255. return -1;
  256. mutex_lock(&obj->sem);
  257. if (obj->suspend != suspend) {
  258. /* GPS_DBG("issue sysfs_notify : %p\n", obj->kobj->sd); */
  259. sysfs_notify(obj->kobj, NULL, "suspend");
  260. }
  261. obj->suspend = suspend;
  262. mutex_unlock(&obj->sem);
  263. return 0;
  264. }
  265. /******************************************************************************/
  266. static inline int mt3326_gps_set_pwrctl(struct gps_drv_obj *obj, unsigned char pwrctl)
  267. {
  268. int err = 0;
  269. if (!obj)
  270. return -1;
  271. mutex_lock(&obj->sem);
  272. if ((pwrctl == GPS_PWRCTL_ON) || (pwrctl == GPS_PWRCTL_OFF)) {
  273. obj->pwrctl = pwrctl;
  274. mt3326_gps_power(obj->hw, pwrctl, 0);
  275. } else if (pwrctl == GPS_PWRCTL_OFF_FORCE) {
  276. obj->pwrctl = pwrctl;
  277. mt3326_gps_power(obj->hw, pwrctl, 1);
  278. } else if (pwrctl == GPS_PWRCTL_RST) {
  279. mt3326_gps_reset(obj->hw, obj->rdelay, 0);
  280. obj->pwrctl = GPS_PWRCTL_ON;
  281. } else if (pwrctl == GPS_PWRCTL_RST_FORCE) {
  282. mt3326_gps_reset(obj->hw, obj->rdelay, 1);
  283. obj->pwrctl = GPS_PWRCTL_ON;
  284. } else {
  285. err = -1;
  286. }
  287. mutex_unlock(&obj->sem);
  288. return err;
  289. }
  290. /******************************************************************************/
  291. static inline int mt3326_gps_set_status(struct gps_drv_obj *obj, const char *buf, size_t count)
  292. {
  293. int err = 0;
  294. int year, mon, day, hour, minute, sec, cnt, reason, idx;
  295. if (!obj)
  296. return -1;
  297. mutex_lock(&obj->sem);
  298. if (sscanf(buf, "(%d/%d/%d %d:%d:%d) - %d/%d", &year, &mon, &day, &hour, &minute, &sec, &cnt, &reason) == 8) {
  299. int number = (int)(sizeof(obj->status.items) / sizeof(obj->status.items[0]));
  300. idx = obj->status.index % number;
  301. obj->status.items[idx].year = (unsigned char)year;
  302. obj->status.items[idx].month = (unsigned char)mon;
  303. obj->status.items[idx].day = (unsigned char)day;
  304. obj->status.items[idx].hour = (unsigned char)hour;
  305. obj->status.items[idx].minute = (unsigned char)minute;
  306. obj->status.items[idx].sec = (unsigned char)sec;
  307. obj->status.items[idx].count = (unsigned char)cnt;
  308. obj->status.items[idx].reason = (unsigned char)reason;
  309. obj->status.index++;
  310. } else {
  311. err = -1;
  312. }
  313. mutex_unlock(&obj->sem);
  314. return err;
  315. }
  316. /******************************************************************************/
  317. static inline int mt3326_gps_set_state(struct gps_drv_obj *obj, unsigned char state)
  318. {
  319. int err = 0;
  320. if (!obj)
  321. return -1;
  322. mutex_lock(&obj->sem);
  323. if (state < GPS_STATE_MAX)
  324. obj->state = state;
  325. else
  326. err = -1;
  327. mutex_unlock(&obj->sem);
  328. return err;
  329. }
  330. /******************************************************************************/
  331. static inline int mt3326_gps_set_pwrsave(struct gps_drv_obj *obj, unsigned char pwrsave)
  332. {
  333. int err = 0;
  334. if (!obj)
  335. return -1;
  336. mutex_lock(&obj->sem);
  337. if (pwrsave < GPS_PWRSAVE_MAX)
  338. obj->pwrsave = pwrsave;
  339. else
  340. err = -1;
  341. mutex_unlock(&obj->sem);
  342. return err;
  343. }
  344. /******************************************************************************/
  345. static inline int mt3326_gps_dev_suspend(struct gps_drv_obj *obj)
  346. {
  347. #if defined(GPS_SUSPEND_RESUME)
  348. int err;
  349. err = mt3326_gps_set_suspend(obj, GPS_PWR_SUSPEND);
  350. if (err)
  351. GPS_DBG("set suspend fail: %d\n", err);
  352. err = mt3326_gps_set_pwrctl(obj, GPS_PWRCTL_OFF);
  353. if (err)
  354. GPS_DBG("set pwrctl fail: %d\n", err);
  355. return err;
  356. #endif
  357. }
  358. /******************************************************************************/
  359. static inline int mt3326_gps_dev_resume(struct gps_drv_obj *obj)
  360. {
  361. #if defined(GPS_SUSPEND_RESUME)
  362. int err;
  363. err = mt3326_gps_set_suspend(obj, GPS_PWR_RESUME);
  364. if (err)
  365. GPS_DBG("set suspend fail: %d\n", err);
  366. /*don't power on device automatically */
  367. return err;
  368. #endif
  369. }
  370. /******************************************************************************/
  371. static ssize_t mt3326_show_pwrctl(struct device *dev, struct device_attribute *attr, char *buf)
  372. {
  373. struct gps_drv_obj *obj;
  374. ssize_t res;
  375. if (!dev) {
  376. GPS_DBG("dev is null!!\n");
  377. return 0;
  378. }
  379. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  380. if (!obj) {
  381. GPS_DBG("drv data is null!!\n");
  382. return 0;
  383. }
  384. mutex_lock(&obj->sem);
  385. res = snprintf(buf, PAGE_SIZE, "%d\n", obj->pwrctl);
  386. mutex_unlock(&obj->sem);
  387. return res;
  388. }
  389. /******************************************************************************/
  390. static ssize_t mt3326_store_pwrctl(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  391. {
  392. struct gps_drv_obj *obj;
  393. if (!dev) {
  394. GPS_DBG("dev is null!!\n");
  395. return 0;
  396. }
  397. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  398. if (!obj) {
  399. GPS_DBG("drv data is null!!\n");
  400. return 0;
  401. }
  402. if ((count == 1) || ((count == 2) && (buf[1] == '\n'))) {
  403. unsigned char pwrctl = buf[0] - '0';
  404. if (!mt3326_gps_set_pwrctl(obj, pwrctl))
  405. return count;
  406. }
  407. /* GPS_DBG("invalid content: '%s', length = %ld\n", buf, count); */
  408. return count;
  409. }
  410. /******************************************************************************/
  411. static ssize_t mt3326_show_suspend(struct device *dev, struct device_attribute *attr, char *buf)
  412. {
  413. struct gps_drv_obj *obj;
  414. ssize_t res;
  415. if (!dev) {
  416. GPS_DBG("dev is null!!\n");
  417. return 0;
  418. }
  419. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  420. if (!obj) {
  421. GPS_DBG("drv data is null!!\n");
  422. return 0;
  423. }
  424. mutex_lock(&obj->sem);
  425. res = snprintf(buf, PAGE_SIZE, "%d\n", obj->suspend);
  426. mutex_unlock(&obj->sem);
  427. return res;
  428. }
  429. /******************************************************************************/
  430. static ssize_t mt3326_store_suspend(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  431. {
  432. struct gps_drv_obj *obj;
  433. if (!dev) {
  434. GPS_DBG("dev is null!!\n");
  435. return 0;
  436. }
  437. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  438. if (!obj) {
  439. GPS_DBG("drv data is null!!\n");
  440. return 0;
  441. }
  442. if ((count == 1) || ((count == 2) && (buf[1] == '\n'))) {
  443. unsigned char suspend = buf[0] - '0';
  444. if (suspend == GPS_PWR_SUSPEND) {
  445. if (!mt3326_gps_dev_suspend(obj))
  446. return count;
  447. } else if (suspend == GPS_PWR_RESUME) {
  448. if (!mt3326_gps_dev_resume(obj))
  449. return count;
  450. }
  451. }
  452. /* GPS_DBG("invalid content: '%s', length = %d\n", buf, count); */
  453. return count;
  454. }
  455. /******************************************************************************/
  456. static ssize_t mt3326_show_status(struct device *dev, struct device_attribute *attr, char *buf)
  457. {
  458. int res, idx, num, left, cnt, len;
  459. struct gps_drv_obj *obj;
  460. char *reason = NULL;
  461. int reason_max = (int)(sizeof(str_reason) / sizeof(str_reason[0]));
  462. if (!dev) {
  463. GPS_DBG("dev is null!!\n");
  464. return 0;
  465. }
  466. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  467. if (!obj) {
  468. GPS_DBG("drv data is null!!\n");
  469. return 0;
  470. }
  471. mutex_lock(&obj->sem);
  472. num = (int)(sizeof(obj->status.items) / sizeof(obj->status.items[0]));
  473. left = PAGE_SIZE;
  474. cnt = 0;
  475. len = 0;
  476. for (idx = 0; idx < num; idx++) {
  477. if (obj->status.items[idx].month == 0)
  478. continue;
  479. if (obj->status.items[idx].reason >= reason_max)
  480. reason = str_reason[reason_max - 1];
  481. else
  482. reason = str_reason[obj->status.items[idx].reason];
  483. cnt =
  484. snprintf(buf + len, left,
  485. "[%d] %.4d/%.2d/%.2d %.2d:%.2d:%.2d - %d, %s\n",
  486. idx, obj->status.items[idx].year + 1900,
  487. obj->status.items[idx].month,
  488. obj->status.items[idx].day,
  489. obj->status.items[idx].hour,
  490. obj->status.items[idx].minute,
  491. obj->status.items[idx].sec, obj->status.items[idx].count, reason);
  492. left -= cnt;
  493. len += cnt;
  494. }
  495. res = PAGE_SIZE - left;
  496. mutex_unlock(&obj->sem);
  497. return res;
  498. }
  499. /******************************************************************************/
  500. static ssize_t mt3326_store_status(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  501. {
  502. int res = 0;
  503. struct gps_drv_obj *obj;
  504. if (!dev) {
  505. GPS_DBG("dev is null!!\n");
  506. return 0;
  507. }
  508. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  509. if (!obj) {
  510. GPS_DBG("drv data is null!!\n");
  511. return 0;
  512. }
  513. res = mt3326_gps_set_status(obj, buf, count);
  514. if (!res)
  515. return count;
  516. GPS_DBG("invalid content: '%p', length = %zu\n", buf, count);
  517. return count;
  518. }
  519. /******************************************************************************/
  520. static ssize_t mt3326_show_state(struct device *dev, struct device_attribute *attr, char *buf)
  521. {
  522. ssize_t res;
  523. struct gps_drv_obj *obj;
  524. if (!dev) {
  525. GPS_DBG("dev is null!!\n");
  526. return 0;
  527. }
  528. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  529. if (!obj) {
  530. GPS_DBG("drv data is null!!\n");
  531. return 0;
  532. }
  533. mutex_lock(&obj->sem);
  534. res = snprintf(buf, PAGE_SIZE, "%d\n", obj->state);
  535. mutex_unlock(&obj->sem);
  536. return res;
  537. }
  538. /******************************************************************************/
  539. static ssize_t mt3326_store_state(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  540. {
  541. struct gps_drv_obj *obj;
  542. if (!dev) {
  543. GPS_DBG("dev is null!!\n");
  544. return 0;
  545. }
  546. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  547. if (!obj) {
  548. GPS_DBG("drv data is null!!\n");
  549. return 0;
  550. }
  551. if ((count == 1) || ((count == 2) && (buf[1] == '\n'))) { /*To Do: dynamic change according to input */
  552. unsigned char state = buf[0] - '0';
  553. if (!mt3326_gps_set_state(obj, state))
  554. return count;
  555. }
  556. GPS_DBG("invalid content: '%p', length = %zu\n", buf, count);
  557. return count;
  558. }
  559. /******************************************************************************/
  560. static ssize_t mt3326_show_pwrsave(struct device *dev, struct device_attribute *attr, char *buf)
  561. {
  562. ssize_t res;
  563. struct gps_drv_obj *obj;
  564. if (!dev) {
  565. GPS_DBG("dev is null!!\n");
  566. return 0;
  567. }
  568. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  569. if (!obj) {
  570. GPS_DBG("drv data is null!!\n");
  571. return 0;
  572. }
  573. mutex_lock(&obj->sem);
  574. res = snprintf(buf, PAGE_SIZE, "%d\n", obj->pwrsave);
  575. mutex_unlock(&obj->sem);
  576. return res;
  577. }
  578. /******************************************************************************/
  579. static ssize_t mt3326_store_pwrsave(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  580. {
  581. struct gps_drv_obj *obj;
  582. if (!dev) {
  583. GPS_DBG("dev is null!!\n");
  584. return 0;
  585. }
  586. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  587. if (!obj) {
  588. GPS_DBG("drv data is null!!\n");
  589. return 0;
  590. }
  591. if ((count == 1) || ((count == 2) && (buf[1] == '\n'))) {
  592. unsigned char pwrsave = buf[0] - '0';
  593. if (!mt3326_gps_set_pwrsave(obj, pwrsave))
  594. return count;
  595. }
  596. GPS_DBG("invalid content: '%p', length = %zu\n", buf, count);
  597. return count;
  598. }
  599. /******************************************************************************/
  600. #if defined(GPS_CONFIGURABLE_RESET_DELAY)
  601. /******************************************************************************/
  602. static ssize_t mt3326_show_rdelay(struct device *dev, struct device_attribute *attr, char *buf)
  603. {
  604. ssize_t res;
  605. struct gps_drv_obj *obj;
  606. if (!dev) {
  607. GPS_DBG("dev is null!!\n");
  608. return 0;
  609. }
  610. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  611. if (!obj) {
  612. GPS_DBG("drv data is null!!\n");
  613. return 0;
  614. }
  615. mutex_lock(&obj->sem);
  616. res = snprintf(buf, PAGE_SIZE, "%d\n", obj->rdelay);
  617. mutex_unlock(&obj->sem);
  618. return res;
  619. }
  620. /******************************************************************************/
  621. static ssize_t mt3326_store_rdelay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  622. {
  623. struct gps_drv_obj *obj;
  624. int rdelay;
  625. unsigned long val;
  626. if (!dev) {
  627. GPS_DBG("dev is null!!\n");
  628. return 0;
  629. }
  630. obj = (struct gps_drv_obj *)dev_get_drvdata(dev);
  631. if (!obj) {
  632. GPS_DBG("drv data is null!!\n");
  633. return 0;
  634. }
  635. rdelay = (int)kstrtol(buf, 10, &val);
  636. if (rdelay < 2000) {
  637. mutex_lock(&obj->sem);
  638. obj->rdelay = rdelay;
  639. mutex_unlock(&obj->sem);
  640. return count;
  641. }
  642. GPS_DBG("invalid content: '%p', length = %zu\n", buf, count);
  643. return count;
  644. }
  645. /******************************************************************************/
  646. #endif
  647. /******************************************************************************/
  648. DEVICE_ATTR(pwrctl, S_IWUSR | S_IWGRP | S_IRUGO, mt3326_show_pwrctl, mt3326_store_pwrctl);
  649. DEVICE_ATTR(suspend, S_IWUSR | S_IWGRP | S_IRUGO, mt3326_show_suspend, mt3326_store_suspend);
  650. DEVICE_ATTR(status, S_IWUSR | S_IWGRP | S_IRUGO, mt3326_show_status, mt3326_store_status);
  651. DEVICE_ATTR(state, S_IWUSR | S_IWGRP | S_IRUGO, mt3326_show_state, mt3326_store_state);
  652. DEVICE_ATTR(pwrsave, S_IWUSR | S_IWGRP | S_IRUGO, mt3326_show_pwrsave, mt3326_store_pwrsave);
  653. #if defined(GPS_CONFIGURABLE_RESET_DELAY)
  654. DEVICE_ATTR(rdelay, S_IWUSR | S_IWGRP | S_IRUGO, mt3326_show_rdelay, mt3326_store_rdelay);
  655. #endif
  656. static struct device_attribute *gps_attr_list[] = {
  657. &dev_attr_pwrctl,
  658. &dev_attr_suspend,
  659. &dev_attr_status,
  660. &dev_attr_state,
  661. &dev_attr_pwrsave,
  662. #if defined(GPS_CONFIGURABLE_RESET_DELAY)
  663. &dev_attr_rdelay,
  664. #endif
  665. };
  666. /******************************************************************************/
  667. static int mt3326_gps_create_attr(struct device *dev)
  668. {
  669. int idx, err = 0;
  670. int num = (int)(sizeof(gps_attr_list) / sizeof(gps_attr_list[0]));
  671. if (!dev)
  672. return -EINVAL;
  673. GPS_TRC();
  674. for (idx = 0; idx < num; idx++) {
  675. err = device_create_file(dev, gps_attr_list[idx]);
  676. if (err) {
  677. GPS_DBG("device_create_file (%s) = %d\n", gps_attr_list[idx]->attr.name, err);
  678. break;
  679. }
  680. }
  681. return err;
  682. }
  683. /******************************************************************************/
  684. static int mt3326_gps_delete_attr(struct device *dev)
  685. {
  686. int idx, err = 0;
  687. int num = (int)(sizeof(gps_attr_list) / sizeof(gps_attr_list[0]));
  688. if (!dev)
  689. return -EINVAL;
  690. for (idx = 0; idx < num; idx++)
  691. device_remove_file(dev, gps_attr_list[idx]);
  692. return err;
  693. }
  694. /******************************************************************************/
  695. long mt3326_gps_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  696. {
  697. GPS_DBG("mt3326_gps_unlocked_ioctl!!\n");
  698. return -ENOIOCTLCMD;
  699. }
  700. /******************************************************************************/
  701. long mt3326_gps_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  702. {
  703. long ret;
  704. GPS_DBG("mt3326_gps_compat_ioctl!!\n");
  705. ret = mt3326_gps_unlocked_ioctl(file, cmd, arg);
  706. return ret;
  707. }
  708. /*****************************************************************************/
  709. static int mt3326_gps_open(struct inode *inode, struct file *file)
  710. {
  711. GPS_TRC();
  712. file->private_data = &gps_private; /* all files share the same buffer */
  713. return nonseekable_open(inode, file);
  714. }
  715. /*****************************************************************************/
  716. static int mt3326_gps_release(struct inode *inode, struct file *file)
  717. {
  718. struct gps_data *dev = file->private_data;
  719. GPS_TRC();
  720. if (dev)
  721. file->private_data = NULL;
  722. return 0;
  723. }
  724. /******************************************************************************/
  725. static ssize_t mt3326_gps_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  726. {
  727. struct gps_data *dev = file->private_data;
  728. ssize_t ret = 0;
  729. int copy_len = 0;
  730. GPS_TRC();
  731. if (!dev)
  732. return -EINVAL;
  733. if (signal_pending(current))
  734. return -ERESTARTSYS;
  735. if (down_interruptible(&dev->sem))
  736. return -ERESTARTSYS;
  737. if (dev->dat_len == 0) { /*no data to be read */
  738. up(&dev->sem);
  739. if (file->f_flags & O_NONBLOCK) /*non-block mode */
  740. return -EAGAIN;
  741. do { /*block mode */
  742. ret = wait_event_interruptible(dev->read_wait, (dev->dat_len > 0));
  743. if (ret == -ERESTARTSYS)
  744. return -ERESTARTSYS;
  745. } while (ret == 0);
  746. if (down_interruptible(&dev->sem))
  747. return -ERESTARTSYS;
  748. }
  749. /*data is available */
  750. copy_len = (dev->dat_len < (int)count) ? (dev->dat_len) : (int)(count);
  751. if (copy_to_user(buf, (dev->dat_buf + dev->dat_pos), (unsigned long)copy_len)) {
  752. ret = -EFAULT;
  753. } else {
  754. /* GPS_VER("mt3326_gps_read(%ld,%d,%d) = %d\n", count, dev->dat_pos, dev->dat_len, */
  755. /* copy_len); */
  756. if (dev->dat_len > (copy_len + dev->dat_pos)) {
  757. dev->dat_pos += copy_len;
  758. } else {
  759. dev->dat_len = 0;
  760. dev->dat_pos = 0;
  761. }
  762. ret = copy_len;
  763. }
  764. up(&dev->sem);
  765. /* GPS_VER("%s return %ld bytes\n", __func__, ret); */
  766. return ret;
  767. }
  768. /******************************************************************************/
  769. static ssize_t mt3326_gps_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  770. {
  771. struct gps_data *dev = file->private_data;
  772. ssize_t ret = 0;
  773. int copy_size = 0;
  774. GPS_TRC();
  775. if (!dev)
  776. return -EINVAL;
  777. if (!count) /*no data written */
  778. return 0;
  779. if (signal_pending(current))
  780. return -ERESTARTSYS;
  781. if (down_interruptible(&dev->sem))
  782. return -ERESTARTSYS;
  783. copy_size = (count < 4096) ? count : 4096;
  784. if (copy_from_user(dev->dat_buf, buf, copy_size)) {
  785. GPS_DBG("copy_from_user error");
  786. ret = -EFAULT;
  787. } else {
  788. dev->dat_len = count;
  789. dev->dat_pos = 0;
  790. ret = count;
  791. }
  792. up(&dev->sem);
  793. wake_up_interruptible(&dev->read_wait);
  794. GPS_VER("%s: write %d bytes\n", __func__, dev->dat_len);
  795. return ret;
  796. }
  797. /******************************************************************************/
  798. static unsigned int mt3326_gps_poll(struct file *file, poll_table *wait)
  799. {
  800. struct gps_data *dev = file->private_data;
  801. unsigned int mask = 0;
  802. GPS_TRC();
  803. if (!dev)
  804. return 0;
  805. down(&dev->sem);
  806. poll_wait(file, &dev->read_wait, wait);
  807. if (dev->dat_len != 0) /*readable if data is available */
  808. mask = (POLLIN | POLLRDNORM) | (POLLOUT | POLLWRNORM);
  809. else /*always writable */
  810. mask = (POLLOUT | POLLWRNORM);
  811. up(&dev->sem);
  812. GPS_VER("%s: mask : 0x%X\n", __func__, mask);
  813. return mask;
  814. }
  815. /*****************************************************************************/
  816. /* Kernel interface */
  817. static const struct file_operations mt3326_gps_fops = {
  818. .owner = THIS_MODULE,
  819. .unlocked_ioctl = mt3326_gps_unlocked_ioctl,
  820. .compat_ioctl = mt3326_gps_compat_ioctl,
  821. .open = mt3326_gps_open,
  822. .read = mt3326_gps_read,
  823. .write = mt3326_gps_write,
  824. .release = mt3326_gps_release,
  825. .poll = mt3326_gps_poll,
  826. };
  827. /*****************************************************************************/
  828. static void mt3326_gps_hw_init(struct mt3326_gps_hardware *hw)
  829. {
  830. mt3326_gps_power(hw, 1, 0);
  831. }
  832. /*****************************************************************************/
  833. static void mt3326_gps_hw_exit(struct mt3326_gps_hardware *hw)
  834. {
  835. mt3326_gps_power(hw, 0, 0);
  836. }
  837. /*****************************************************************************/
  838. static int mt3326_gps_probe(struct platform_device *dev)
  839. {
  840. int ret = 0, err = 0;
  841. struct gps_drv_obj *drvobj = NULL;
  842. /* struct mt3326_gps_hardware *hw = (struct mt3326_gps_hardware *)dev->dev.platform_data; */
  843. struct mt3326_gps_hardware *hw = &mt3326_gps_hw;
  844. struct gps_dev_obj *devobj = NULL;
  845. devobj = kzalloc(sizeof(*devobj), GFP_KERNEL);
  846. if (!devobj) {
  847. GPS_ERR("-ENOMEM\n");
  848. err = -ENOMEM;
  849. return -1;
  850. }
  851. mt3326_gps_hw_init(hw);
  852. GPS_DBG("Registering chardev\n");
  853. ret = alloc_chrdev_region(&devobj->devno, 0, 1, GPS_DEVNAME);
  854. if (ret) {
  855. GPS_ERR("alloc_chrdev_region fail: %d\n", ret);
  856. goto error;
  857. } else {
  858. GPS_DBG("major: %d, minor: %d\n", MAJOR(devobj->devno), MINOR(devobj->devno));
  859. }
  860. cdev_init(&devobj->chdev, &mt3326_gps_fops);
  861. devobj->chdev.owner = THIS_MODULE;
  862. err = cdev_add(&devobj->chdev, devobj->devno, 1);
  863. if (err) {
  864. GPS_ERR("cdev_add fail: %d\n", err);
  865. goto error;
  866. }
  867. drvobj = kmalloc(sizeof(*drvobj), GFP_KERNEL);
  868. if (!drvobj) {
  869. err = -ENOMEM;
  870. goto error;
  871. }
  872. memset(drvobj, 0, sizeof(*drvobj));
  873. devobj->cls = class_create(THIS_MODULE, "gpsdrv");
  874. if (IS_ERR(devobj->cls)) {
  875. GPS_ERR("Unable to create class, err = %d\n", (int)PTR_ERR(devobj->cls));
  876. goto error;
  877. }
  878. devobj->dev = device_create(devobj->cls, NULL, devobj->devno, drvobj, "gps");
  879. drvobj->hw = hw;
  880. drvobj->pwrctl = 0;
  881. drvobj->suspend = 0;
  882. drvobj->state = GPS_STATE_UNSUPPORTED;
  883. drvobj->pwrsave = GPS_PWRSAVE_UNSUPPORTED;
  884. drvobj->rdelay = 50;
  885. drvobj->kobj = &devobj->dev->kobj;
  886. mutex_init(&drvobj->sem);
  887. err = mt3326_gps_create_attr(devobj->dev);
  888. if (err)
  889. goto error;
  890. /*initialize members */
  891. spin_lock_init(&gps_private.lock);
  892. init_waitqueue_head(&gps_private.read_wait);
  893. sema_init(&gps_private.sem, 1);
  894. gps_private.dat_len = 0;
  895. gps_private.dat_pos = 0;
  896. memset(gps_private.dat_buf, 0x00, sizeof(gps_private.dat_buf));
  897. /*set platform data:
  898. a new device created for gps */
  899. platform_set_drvdata(dev, devobj);
  900. GPS_DBG("Done\n");
  901. return 0;
  902. error:
  903. if (err == 0)
  904. cdev_del(&devobj->chdev);
  905. if (ret == 0)
  906. unregister_chrdev_region(devobj->devno, 1);
  907. kfree(devobj);
  908. kfree(drvobj);
  909. return -1;
  910. }
  911. /*****************************************************************************/
  912. static int mt3326_gps_remove(struct platform_device *dev)
  913. {
  914. int err;
  915. struct gps_dev_obj *devobj = (struct gps_dev_obj *)platform_get_drvdata(dev);
  916. struct gps_drv_obj *drvobj;
  917. if (!devobj) {
  918. GPS_ERR("null pointer: %p\n", devobj);
  919. return -1;
  920. }
  921. drvobj = (struct gps_drv_obj *)dev_get_drvdata(devobj->dev);
  922. if (!drvobj) {
  923. GPS_ERR("null pointer: %p\n", drvobj);
  924. return -1;
  925. }
  926. GPS_DBG("Unregistering chardev\n");
  927. cdev_del(&devobj->chdev);
  928. unregister_chrdev_region(devobj->devno, 1);
  929. mt3326_gps_hw_exit(devobj->hw);
  930. err = mt3326_gps_delete_attr(devobj->dev);
  931. if (err)
  932. GPS_ERR("delete attr fails: %d\n", err);
  933. device_destroy(devobj->cls, devobj->devno);
  934. class_destroy(devobj->cls);
  935. kfree(devobj);
  936. GPS_DBG("Done\n");
  937. return 0;
  938. }
  939. /*****************************************************************************/
  940. static void mt3326_gps_shutdown(struct platform_device *dev)
  941. {
  942. struct gps_dev_obj *devobj = (struct gps_dev_obj *)platform_get_drvdata(dev);
  943. GPS_DBG("Shutting down\n");
  944. mt3326_gps_hw_exit(devobj->hw);
  945. }
  946. /*****************************************************************************/
  947. #ifdef CONFIG_PM
  948. /*****************************************************************************/
  949. static int mt3326_gps_suspend(struct platform_device *dev, pm_message_t state)
  950. {
  951. int err = 0;
  952. struct gps_dev_obj *devobj = (struct gps_dev_obj *)platform_get_drvdata(dev);
  953. struct gps_drv_obj *drvobj;
  954. if (!devobj) {
  955. GPS_ERR("null pointer: %p\n", devobj);
  956. return -1;
  957. }
  958. drvobj = (struct gps_drv_obj *)dev_get_drvdata(devobj->dev);
  959. if (!drvobj) {
  960. GPS_ERR("null pointer: %p\n", drvobj);
  961. return -1;
  962. }
  963. if (state.event == PM_EVENT_SUSPEND)
  964. err = mt3326_gps_dev_suspend(drvobj);
  965. else
  966. GPS_DBG("dev = %p, event = %u,", dev, state.event);
  967. return err;
  968. }
  969. /*****************************************************************************/
  970. static int mt3326_gps_resume(struct platform_device *dev)
  971. {
  972. struct gps_dev_obj *devobj = (struct gps_dev_obj *)platform_get_drvdata(dev);
  973. struct gps_drv_obj *drvobj = (struct gps_drv_obj *)dev_get_drvdata(devobj->dev);
  974. return mt3326_gps_dev_resume(drvobj);
  975. }
  976. /*****************************************************************************/
  977. #endif /* CONFIG_PM */
  978. /*****************************************************************************/
  979. #ifdef CONFIG_OF
  980. static const struct of_device_id apgps_of_ids[] = {
  981. {.compatible = "mediatek,mt3326-gps",},
  982. {}
  983. };
  984. #endif
  985. static struct platform_driver mt3326_gps_driver = {
  986. .probe = mt3326_gps_probe,
  987. .remove = mt3326_gps_remove,
  988. .shutdown = mt3326_gps_shutdown,
  989. #if defined(CONFIG_PM)
  990. .suspend = mt3326_gps_suspend,
  991. .resume = mt3326_gps_resume,
  992. #endif
  993. .driver = {
  994. .name = GPS_DEVNAME,
  995. .bus = &platform_bus_type,
  996. #ifdef CONFIG_OF
  997. .of_match_table = apgps_of_ids,
  998. #endif
  999. },
  1000. };
  1001. /*****************************************************************************/
  1002. static int __init mt3326_gps_mod_init(void)
  1003. {
  1004. int ret = 0;
  1005. GPS_TRC();
  1006. /* ret = driver_register(&mt3326_gps_driver); */
  1007. ret = platform_driver_register(&mt3326_gps_driver);
  1008. return ret;
  1009. }
  1010. /*****************************************************************************/
  1011. static void __exit mt3326_gps_mod_exit(void)
  1012. {
  1013. GPS_TRC();
  1014. platform_driver_unregister(&mt3326_gps_driver);
  1015. }
  1016. /*****************************************************************************/
  1017. module_init(mt3326_gps_mod_init);
  1018. module_exit(mt3326_gps_mod_exit);
  1019. /*****************************************************************************/
  1020. MODULE_AUTHOR("MingHsien Hsieh <MingHsien.Hsieh@mediatek.com>");
  1021. MODULE_DESCRIPTION("MT3326 GPS Driver");
  1022. MODULE_LICENSE("GPL");
  1023. #endif