msm_adc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/mutex.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/err.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/hwmon-sysfs.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/fs.h>
  21. #include <linux/sched.h>
  22. #include <linux/wait.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/msm_adc.h>
  25. #include <linux/pmic8058-xoadc.h>
  26. #include <linux/slab.h>
  27. #include <linux/semaphore.h>
  28. #include <linux/module.h>
  29. #include <mach/dal.h>
  30. #define MSM_ADC_DRIVER_NAME "msm_adc"
  31. #define MSM_ADC_MAX_FNAME 15
  32. #define MSM_ADC_DALRPC_DEVICEID 0x02000067
  33. #define MSM_ADC_DALRPC_PORT_NAME "DAL00"
  34. #define MSM_ADC_DALRPC_CPU SMD_APPS_MODEM
  35. #define MSM_ADC_DALRPC_CMD_REQ_CONV 9
  36. #define MSM_ADC_DALRPC_CMD_INPUT_PROP 11
  37. #define MSM_ADC_DALRC_CONV_TIMEOUT (5 * HZ) /* 5 seconds */
  38. #define MSM_8x25_ADC_DEV_ID 0
  39. #define MSM_8x25_CHAN_ID 16
  40. enum dal_error {
  41. DAL_ERROR_INVALID_DEVICE_IDX = 1,
  42. DAL_ERROR_INVALID_CHANNEL_IDX,
  43. DAL_ERROR_NULL_POINTER,
  44. DAL_ERROR_DEVICE_QUEUE_FULL,
  45. DAL_ERROR_INVALID_PROPERTY_LENGTH,
  46. DAL_ERROR_REMOTE_EVENT_POOL_FULL
  47. };
  48. enum dal_result_status {
  49. DAL_RESULT_STATUS_INVALID,
  50. DAL_RESULT_STATUS_VALID
  51. };
  52. struct dal_conv_state {
  53. struct dal_conv_slot context[MSM_ADC_DEV_MAX_INFLIGHT];
  54. struct list_head slots;
  55. struct mutex list_lock;
  56. struct semaphore slot_count;
  57. };
  58. struct adc_dev {
  59. char *name;
  60. uint32_t nchans;
  61. struct dal_conv_state conv;
  62. struct dal_translation transl;
  63. struct sensor_device_attribute *sens_attr;
  64. char **fnames;
  65. };
  66. struct msm_adc_drv {
  67. /* Common to both XOADC and EPM */
  68. struct platform_device *pdev;
  69. struct device *hwmon;
  70. struct miscdevice misc;
  71. /* XOADC variables */
  72. struct sensor_device_attribute *sens_attr;
  73. struct workqueue_struct *wq;
  74. atomic_t online;
  75. atomic_t total_outst;
  76. wait_queue_head_t total_outst_wait;
  77. /* EPM variables */
  78. void *dev_h;
  79. struct adc_dev *devs[MSM_ADC_MAX_NUM_DEVS];
  80. struct mutex prop_lock;
  81. atomic_t rpc_online;
  82. atomic_t rpc_total_outst;
  83. wait_queue_head_t rpc_total_outst_wait;
  84. };
  85. static bool epm_init;
  86. static bool epm_fluid_enabled;
  87. /* Needed to support file_op interfaces */
  88. static struct msm_adc_drv *msm_adc_drv;
  89. static bool conv_first_request;
  90. static ssize_t msm_adc_show_curr(struct device *dev,
  91. struct device_attribute *devattr, char *buf);
  92. static int msm_rpc_adc_blocking_conversion(struct msm_adc_drv *msm_adc,
  93. uint32_t chan, struct adc_chan_result *result);
  94. static int msm_adc_blocking_conversion(struct msm_adc_drv *msm_adc,
  95. uint32_t chan, struct adc_chan_result *result);
  96. static int msm_adc_open(struct inode *inode, struct file *file)
  97. {
  98. struct msm_client_data *client;
  99. struct msm_adc_drv *msm_adc = msm_adc_drv;
  100. struct platform_device *pdev = msm_adc->pdev;
  101. client = kzalloc(sizeof(struct msm_client_data), GFP_KERNEL);
  102. if (!client) {
  103. dev_err(&pdev->dev, "Unable to allocate memory\n");
  104. return -ENOMEM;
  105. }
  106. if (!try_module_get(THIS_MODULE)) {
  107. kfree(client);
  108. return -EACCES;
  109. }
  110. mutex_init(&client->lock);
  111. INIT_LIST_HEAD(&client->complete_list);
  112. init_waitqueue_head(&client->data_wait);
  113. init_waitqueue_head(&client->outst_wait);
  114. client->online = 1;
  115. file->private_data = client;
  116. return nonseekable_open(inode, file);
  117. }
  118. static inline void msm_adc_restore_slot(struct dal_conv_state *conv_s,
  119. struct dal_conv_slot *slot)
  120. {
  121. mutex_lock(&conv_s->list_lock);
  122. list_add(&slot->list, &conv_s->slots);
  123. mutex_unlock(&conv_s->list_lock);
  124. up(&conv_s->slot_count);
  125. }
  126. static int no_pending_client_requests(struct msm_client_data *client)
  127. {
  128. mutex_lock(&client->lock);
  129. if (client->num_outstanding == 0) {
  130. mutex_unlock(&client->lock);
  131. return 1;
  132. }
  133. mutex_unlock(&client->lock);
  134. return 0;
  135. }
  136. static int data_avail(struct msm_client_data *client, uint32_t *pending)
  137. {
  138. uint32_t completed;
  139. mutex_lock(&client->lock);
  140. completed = client->num_complete;
  141. mutex_unlock(&client->lock);
  142. if (completed > 0) {
  143. if (pending != NULL)
  144. *pending = completed;
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static int msm_adc_release(struct inode *inode, struct file *file)
  150. {
  151. struct msm_client_data *client = file->private_data;
  152. struct adc_conv_slot *slot, *tmp;
  153. int rc;
  154. struct msm_adc_platform_data *pdata =
  155. msm_adc_drv->pdev->dev.platform_data;
  156. struct msm_adc_channels *channel = pdata->channel;
  157. module_put(THIS_MODULE);
  158. mutex_lock(&client->lock);
  159. /* prevent any further requests while we teardown the client */
  160. client->online = 0;
  161. mutex_unlock(&client->lock);
  162. /*
  163. * We may still have outstanding transactions in flight from this
  164. * client that have not completed. Make sure they're completed
  165. * before removing the client.
  166. */
  167. rc = wait_event_interruptible(client->outst_wait,
  168. no_pending_client_requests(client));
  169. if (rc) {
  170. pr_err("%s: wait_event_interruptible failed rc = %d\n",
  171. __func__, rc);
  172. return rc;
  173. }
  174. /*
  175. * All transactions have completed. Add slot resources back to the
  176. * appropriate devices.
  177. */
  178. list_for_each_entry_safe(slot, tmp, &client->complete_list, list) {
  179. slot->client = NULL;
  180. list_del(&slot->list);
  181. channel[slot->conv.result.chan].adc_access_fn->adc_restore_slot(
  182. channel[slot->conv.result.chan].adc_dev_instance, slot);
  183. }
  184. kfree(client);
  185. return 0;
  186. }
  187. static int msm_adc_translate_dal_to_hwmon(struct msm_adc_drv *msm_adc,
  188. uint32_t chan,
  189. struct adc_dev_spec *dest)
  190. {
  191. struct dal_translation *transl;
  192. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  193. int i;
  194. for (i = 0; i < pdata->num_adc; i++) {
  195. transl = &msm_adc->devs[i]->transl;
  196. if (chan >= transl->hwmon_start &&
  197. chan <= transl->hwmon_end) {
  198. dest->dal.dev_idx = transl->dal_dev_idx;
  199. dest->hwmon_dev_idx = transl->hwmon_dev_idx;
  200. dest->dal.chan_idx = chan - transl->hwmon_start;
  201. return 0;
  202. }
  203. }
  204. return -EINVAL;
  205. }
  206. static int msm_adc_translate_hwmon_to_dal(struct msm_adc_drv *msm_adc,
  207. struct adc_dev_spec *source,
  208. uint32_t *chan)
  209. {
  210. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  211. struct dal_translation *transl;
  212. int i;
  213. for (i = 0; i < pdata->num_adc; i++) {
  214. transl = &msm_adc->devs[i]->transl;
  215. if (source->dal.dev_idx != transl->dal_dev_idx)
  216. continue;
  217. *chan = transl->hwmon_start + source->dal.chan_idx;
  218. return 0;
  219. }
  220. return -EINVAL;
  221. }
  222. static int msm_adc_getinputproperties(struct msm_adc_drv *msm_adc,
  223. const char *lookup_name,
  224. struct adc_dev_spec *result)
  225. {
  226. struct device *dev = &msm_adc->pdev->dev;
  227. int rc;
  228. mutex_lock(&msm_adc->prop_lock);
  229. rc = dalrpc_fcn_8(MSM_ADC_DALRPC_CMD_INPUT_PROP, msm_adc->dev_h,
  230. lookup_name, strlen(lookup_name) + 1,
  231. &result->dal, sizeof(struct dal_dev_spec));
  232. if (rc) {
  233. dev_err(dev, "DAL getprop request failed: rc = %d\n", rc);
  234. mutex_unlock(&msm_adc->prop_lock);
  235. return -EIO;
  236. }
  237. mutex_unlock(&msm_adc->prop_lock);
  238. return rc;
  239. }
  240. static int msm_adc_lookup(struct msm_adc_drv *msm_adc,
  241. struct msm_adc_lookup *lookup)
  242. {
  243. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  244. struct adc_dev_spec target;
  245. int rc = 0, i = 0;
  246. uint32_t len = 0;
  247. len = strnlen(lookup->name, MSM_ADC_MAX_CHAN_STR);
  248. while (i < pdata->num_chan_supported) {
  249. if (strncmp(lookup->name, pdata->channel[i].name, len))
  250. i++;
  251. else
  252. break;
  253. }
  254. if (pdata->num_chan_supported > 0 && i < pdata->num_chan_supported) {
  255. lookup->chan_idx = i;
  256. } else if (msm_adc->dev_h) {
  257. rc = msm_adc_getinputproperties(msm_adc, lookup->name, &target);
  258. if (rc) {
  259. pr_err("%s: Lookup failed for %s\n", __func__,
  260. lookup->name);
  261. return rc;
  262. }
  263. rc = msm_adc_translate_hwmon_to_dal(msm_adc, &target,
  264. &lookup->chan_idx);
  265. if (rc)
  266. pr_err("%s: Translation failed for %s\n", __func__,
  267. lookup->name);
  268. } else {
  269. pr_err("%s: Lookup failed for %s\n", __func__, lookup->name);
  270. rc = -EINVAL;
  271. }
  272. return rc;
  273. }
  274. static int msm_adc_aio_conversion(struct msm_adc_drv *msm_adc,
  275. struct adc_chan_result *request,
  276. struct msm_client_data *client)
  277. {
  278. struct msm_adc_platform_data *pdata =
  279. msm_adc_drv->pdev->dev.platform_data;
  280. struct msm_adc_channels *channel = &pdata->channel[request->chan];
  281. struct adc_conv_slot *slot;
  282. /* we could block here, but only for a bounded time */
  283. channel->adc_access_fn->adc_slot_request(channel->adc_dev_instance,
  284. &slot);
  285. if (slot) {
  286. atomic_inc(&msm_adc->total_outst);
  287. mutex_lock(&client->lock);
  288. client->num_outstanding++;
  289. mutex_unlock(&client->lock);
  290. /* indicates non blocking request to callback handler */
  291. slot->blocking = 0;
  292. slot->compk = NULL;/*For kernel space usage; n/a for usr space*/
  293. slot->conv.result.chan = client->adc_chan = request->chan;
  294. slot->client = client;
  295. slot->adc_request = START_OF_CONV;
  296. slot->chan_path = channel->chan_path_type;
  297. slot->chan_adc_config = channel->adc_config_type;
  298. slot->chan_adc_calib = channel->adc_calib_type;
  299. queue_work(msm_adc->wq, &slot->work);
  300. return 0;
  301. }
  302. return -EBUSY;
  303. }
  304. static int msm_adc_fluid_hw_deinit(struct msm_adc_drv *msm_adc)
  305. {
  306. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  307. if (!epm_init)
  308. return -EINVAL;
  309. if (pdata->gpio_config == APROC_CONFIG &&
  310. epm_fluid_enabled && pdata->adc_fluid_disable != NULL) {
  311. pdata->adc_fluid_disable();
  312. epm_fluid_enabled = false;
  313. }
  314. return 0;
  315. }
  316. static int msm_adc_fluid_hw_init(struct msm_adc_drv *msm_adc)
  317. {
  318. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  319. if (!epm_init)
  320. return -EINVAL;
  321. if (!pdata->adc_fluid_enable)
  322. return -ENODEV;
  323. printk(KERN_DEBUG "msm_adc_fluid_hw_init: Calling adc_fluid_enable.\n");
  324. if (pdata->gpio_config == APROC_CONFIG && !epm_fluid_enabled) {
  325. pdata->adc_fluid_enable();
  326. epm_fluid_enabled = true;
  327. }
  328. /* return success for now but check for errors from hw init configuration */
  329. return 0;
  330. }
  331. static int msm_adc_poll_complete(struct msm_adc_drv *msm_adc,
  332. struct msm_client_data *client, uint32_t *pending)
  333. {
  334. int rc;
  335. /*
  336. * Don't proceed if there there's nothing queued on this client.
  337. * We could deadlock otherwise in a single threaded scenario.
  338. */
  339. if (no_pending_client_requests(client) && !data_avail(client, pending))
  340. return -EDEADLK;
  341. rc = wait_event_interruptible(client->data_wait,
  342. data_avail(client, pending));
  343. if (rc)
  344. return rc;
  345. return 0;
  346. }
  347. static int msm_adc_read_result(struct msm_adc_drv *msm_adc,
  348. struct msm_client_data *client,
  349. struct adc_chan_result *result)
  350. {
  351. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  352. struct msm_adc_channels *channel = pdata->channel;
  353. struct adc_conv_slot *slot;
  354. int rc = 0;
  355. mutex_lock(&client->lock);
  356. slot = list_first_entry(&client->complete_list,
  357. struct adc_conv_slot, list);
  358. if (!slot) {
  359. mutex_unlock(&client->lock);
  360. return -ENOMSG;
  361. }
  362. slot->client = NULL;
  363. list_del(&slot->list);
  364. client->num_complete--;
  365. mutex_unlock(&client->lock);
  366. *result = slot->conv.result;
  367. /* restore this slot to reserve */
  368. channel[slot->conv.result.chan].adc_access_fn->adc_restore_slot(
  369. channel[slot->conv.result.chan].adc_dev_instance, slot);
  370. return rc;
  371. }
  372. static long msm_adc_ioctl(struct file *file, unsigned int cmd,
  373. unsigned long arg)
  374. {
  375. struct msm_client_data *client = file->private_data;
  376. struct msm_adc_drv *msm_adc = msm_adc_drv;
  377. struct platform_device *pdev = msm_adc->pdev;
  378. struct msm_adc_platform_data *pdata = pdev->dev.platform_data;
  379. uint32_t block_res = 0;
  380. int rc;
  381. switch (cmd) {
  382. case MSM_ADC_REQUEST:
  383. {
  384. struct adc_chan_result conv;
  385. if (copy_from_user(&conv, (void __user *)arg,
  386. sizeof(struct adc_chan_result)))
  387. return -EFAULT;
  388. if (conv.chan < pdata->num_chan_supported) {
  389. rc = msm_adc_blocking_conversion(msm_adc,
  390. conv.chan, &conv);
  391. } else {
  392. if (!msm_adc->dev_h)
  393. return -EAGAIN;
  394. rc = msm_rpc_adc_blocking_conversion(msm_adc,
  395. conv.chan, &conv);
  396. }
  397. if (rc) {
  398. dev_dbg(&pdev->dev, "BLK conversion failed\n");
  399. return rc;
  400. }
  401. if (copy_to_user((void __user *)arg, &conv,
  402. sizeof(struct adc_chan_result)))
  403. return -EFAULT;
  404. break;
  405. }
  406. case MSM_ADC_AIO_REQUEST_BLOCK_RES:
  407. block_res = 1;
  408. case MSM_ADC_AIO_REQUEST:
  409. {
  410. struct adc_chan_result conv;
  411. if (copy_from_user(&conv, (void __user *)arg,
  412. sizeof(struct adc_chan_result)))
  413. return -EFAULT;
  414. if (conv.chan >= pdata->num_chan_supported)
  415. return -EINVAL;
  416. rc = msm_adc_aio_conversion(msm_adc, &conv, client);
  417. if (rc) {
  418. dev_dbg(&pdev->dev, "AIO conversion failed\n");
  419. return rc;
  420. }
  421. if (copy_to_user((void __user *)arg, &conv,
  422. sizeof(struct adc_chan_result)))
  423. return -EFAULT;
  424. break;
  425. }
  426. case MSM_ADC_AIO_POLL:
  427. {
  428. uint32_t completed;
  429. rc = msm_adc_poll_complete(msm_adc, client, &completed);
  430. if (rc) {
  431. dev_dbg(&pdev->dev, "poll request failed\n");
  432. return rc;
  433. }
  434. if (copy_to_user((void __user *)arg, &completed,
  435. sizeof(uint32_t)))
  436. return -EFAULT;
  437. break;
  438. }
  439. case MSM_ADC_AIO_READ:
  440. {
  441. struct adc_chan_result result;
  442. rc = msm_adc_read_result(msm_adc, client, &result);
  443. if (rc) {
  444. dev_dbg(&pdev->dev, "read result failed\n");
  445. return rc;
  446. }
  447. if (copy_to_user((void __user *)arg, &result,
  448. sizeof(struct adc_chan_result)))
  449. return -EFAULT;
  450. break;
  451. }
  452. case MSM_ADC_LOOKUP:
  453. {
  454. struct msm_adc_lookup lookup;
  455. if (copy_from_user(&lookup, (void __user *)arg,
  456. sizeof(struct msm_adc_lookup)))
  457. return -EFAULT;
  458. rc = msm_adc_lookup(msm_adc, &lookup);
  459. if (rc) {
  460. dev_dbg(&pdev->dev, "No such channel: %s\n",
  461. lookup.name);
  462. return rc;
  463. }
  464. if (copy_to_user((void __user *)arg, &lookup,
  465. sizeof(struct msm_adc_lookup)))
  466. return -EFAULT;
  467. break;
  468. }
  469. case MSM_ADC_FLUID_INIT:
  470. {
  471. uint32_t result;
  472. result = msm_adc_fluid_hw_init(msm_adc);
  473. if (copy_to_user((void __user *)arg, &result,
  474. sizeof(uint32_t))) {
  475. printk(KERN_ERR "MSM_ADC_FLUID_INIT: "
  476. "copy_to_user returned an error.\n");
  477. return -EFAULT;
  478. }
  479. printk(KERN_DEBUG "MSM_ADC_FLUID_INIT: Success.\n");
  480. break;
  481. }
  482. case MSM_ADC_FLUID_DEINIT:
  483. {
  484. uint32_t result;
  485. result = msm_adc_fluid_hw_deinit(msm_adc);
  486. if (copy_to_user((void __user *)arg, &result,
  487. sizeof(uint32_t)))
  488. return -EFAULT;
  489. break;
  490. }
  491. default:
  492. return -EINVAL;
  493. }
  494. return 0;
  495. }
  496. const struct file_operations msm_adc_fops = {
  497. .open = msm_adc_open,
  498. .release = msm_adc_release,
  499. .unlocked_ioctl = msm_adc_ioctl,
  500. };
  501. static ssize_t msm_adc_show_curr(struct device *dev,
  502. struct device_attribute *devattr, char *buf)
  503. {
  504. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  505. struct msm_adc_drv *msm_adc = dev_get_drvdata(dev);
  506. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  507. struct adc_chan_result result;
  508. int rc;
  509. #ifdef CONFIG_PMIC8058_XOADC
  510. rc = pm8058_xoadc_registered();
  511. if (rc <= 0)
  512. return -ENODEV;
  513. #endif
  514. if (attr->index < pdata->num_chan_supported) {
  515. rc = msm_adc_blocking_conversion(msm_adc,
  516. attr->index, &result);
  517. } else {
  518. if (pdata->gpio_config == APROC_CONFIG && !epm_fluid_enabled
  519. && pdata->adc_fluid_enable != NULL) {
  520. printk(KERN_DEBUG "This is to read ADC value for "
  521. "Fluid EPM and init. Do it only once.\n");
  522. pdata->adc_fluid_enable();
  523. epm_fluid_enabled = true;
  524. }
  525. rc = msm_rpc_adc_blocking_conversion(msm_adc,
  526. attr->index, &result);
  527. }
  528. if (rc)
  529. return 0;
  530. return sprintf(buf, "Result: %lld Raw: %d\n", result.physical,
  531. result.adc_code);
  532. }
  533. static int msm_rpc_adc_blocking_conversion(struct msm_adc_drv *msm_adc,
  534. uint32_t hwmon_chan, struct adc_chan_result *result)
  535. {
  536. struct msm_adc_platform_data *pdata = msm_adc->pdev->dev.platform_data;
  537. struct dal_conv_request params;
  538. struct device *dev = &msm_adc->pdev->dev;
  539. struct adc_dev *adc_dev;
  540. struct dal_conv_state *conv_s;
  541. struct dal_conv_slot *slot;
  542. struct adc_dev_spec dest;
  543. int timeout, rc = 0;
  544. if (pdata->gpio_config == APROC_CONFIG &&
  545. pdata->adc_gpio_enable != NULL)
  546. pdata->adc_gpio_enable(hwmon_chan-pdata->num_chan_supported);
  547. rc = msm_adc_translate_dal_to_hwmon(msm_adc, hwmon_chan, &dest);
  548. if (rc) {
  549. dev_err(dev, "%s: translation from chan %u failed\n",
  550. __func__, hwmon_chan);
  551. if (pdata->gpio_config == APROC_CONFIG &&
  552. pdata->adc_gpio_disable != NULL)
  553. pdata->adc_gpio_disable(hwmon_chan
  554. -pdata->num_chan_supported);
  555. return -EINVAL;
  556. }
  557. adc_dev = msm_adc->devs[dest.hwmon_dev_idx];
  558. conv_s = &adc_dev->conv;
  559. down(&conv_s->slot_count);
  560. mutex_lock(&conv_s->list_lock);
  561. slot = list_first_entry(&conv_s->slots, struct dal_conv_slot, list);
  562. list_del(&slot->list);
  563. BUG_ON(!slot);
  564. mutex_unlock(&conv_s->list_lock);
  565. /* indicates blocking request to callback handler */
  566. slot->blocking = 1;
  567. params.target.dev_idx = dest.dal.dev_idx;
  568. params.target.chan_idx = dest.dal.chan_idx;
  569. params.cb_h = slot->cb_h;
  570. rc = dalrpc_fcn_8(MSM_ADC_DALRPC_CMD_REQ_CONV, msm_adc->dev_h,
  571. &params, sizeof(params), NULL, 0);
  572. if (rc) {
  573. dev_err(dev, "%s: Conversion for device = %u channel = %u"
  574. " failed\n", __func__, params.target.dev_idx,
  575. params.target.chan_idx);
  576. rc = -EIO;
  577. goto blk_conv_err;
  578. }
  579. timeout = wait_for_completion_interruptible_timeout(&slot->comp,
  580. MSM_ADC_DALRC_CONV_TIMEOUT);
  581. if (timeout == 0) {
  582. dev_err(dev, "read for device = %u channel = %u timed out\n",
  583. params.target.dev_idx, params.target.chan_idx);
  584. rc = -ETIMEDOUT;
  585. goto blk_conv_err;
  586. } else if (timeout < 0) {
  587. rc = -EINTR;
  588. goto blk_conv_err;
  589. }
  590. result->physical = (int64_t)slot->result.physical;
  591. if (slot->result.status == DAL_RESULT_STATUS_INVALID)
  592. rc = -ENODATA;
  593. blk_conv_err:
  594. if (pdata->gpio_config == APROC_CONFIG &&
  595. pdata->adc_gpio_disable != NULL)
  596. pdata->adc_gpio_disable(hwmon_chan-pdata->num_chan_supported);
  597. msm_adc_restore_slot(conv_s, slot);
  598. return rc;
  599. }
  600. static int msm_adc_blocking_conversion(struct msm_adc_drv *msm_adc,
  601. uint32_t hwmon_chan, struct adc_chan_result *result)
  602. {
  603. struct adc_conv_slot *slot;
  604. struct msm_adc_platform_data *pdata =
  605. msm_adc_drv->pdev->dev.platform_data;
  606. struct msm_adc_channels *channel = &pdata->channel[hwmon_chan];
  607. int ret = 0;
  608. if (conv_first_request) {
  609. ret = pm8058_xoadc_calib_device(channel->adc_dev_instance);
  610. if (ret) {
  611. pr_err("pmic8058 xoadc calibration failed, retry\n");
  612. return ret;
  613. }
  614. conv_first_request = false;
  615. }
  616. channel->adc_access_fn->adc_slot_request(channel->adc_dev_instance,
  617. &slot);
  618. if (slot) {
  619. slot->conv.result.chan = hwmon_chan;
  620. /* indicates blocking request to callback handler */
  621. slot->blocking = 1;
  622. slot->adc_request = START_OF_CONV;
  623. slot->chan_path = channel->chan_path_type;
  624. slot->chan_adc_config = channel->adc_config_type;
  625. slot->chan_adc_calib = channel->adc_calib_type;
  626. queue_work(msm_adc_drv->wq, &slot->work);
  627. wait_for_completion_interruptible(&slot->comp);
  628. *result = slot->conv.result;
  629. channel->adc_access_fn->adc_restore_slot(
  630. channel->adc_dev_instance, slot);
  631. return 0;
  632. }
  633. return -EBUSY;
  634. }
  635. int32_t adc_channel_open(uint32_t channel, void **h)
  636. {
  637. struct msm_client_data *client;
  638. struct msm_adc_drv *msm_adc = msm_adc_drv;
  639. struct msm_adc_platform_data *pdata;
  640. struct platform_device *pdev;
  641. int i = 0;
  642. if (!msm_adc_drv)
  643. return -EFAULT;
  644. #ifdef CONFIG_PMIC8058_XOADC
  645. if (pm8058_xoadc_registered() <= 0)
  646. return -ENODEV;
  647. #endif
  648. pdata = msm_adc->pdev->dev.platform_data;
  649. pdev = msm_adc->pdev;
  650. while (i < pdata->num_chan_supported) {
  651. if (channel == pdata->channel[i].channel_name)
  652. break;
  653. else
  654. i++;
  655. }
  656. if (i == pdata->num_chan_supported)
  657. return -EBADF; /* unknown channel */
  658. client = kzalloc(sizeof(struct msm_client_data), GFP_KERNEL);
  659. if (!client) {
  660. dev_err(&pdev->dev, "Unable to allocate memory\n");
  661. return -ENOMEM;
  662. }
  663. if (!try_module_get(THIS_MODULE)) {
  664. kfree(client);
  665. return -EACCES;
  666. }
  667. mutex_init(&client->lock);
  668. INIT_LIST_HEAD(&client->complete_list);
  669. init_waitqueue_head(&client->data_wait);
  670. init_waitqueue_head(&client->outst_wait);
  671. client->online = 1;
  672. client->adc_chan = i;
  673. *h = (void *)client;
  674. return 0;
  675. }
  676. int32_t adc_channel_close(void *h)
  677. {
  678. struct msm_client_data *client = (struct msm_client_data *)h;
  679. kfree(client);
  680. return 0;
  681. }
  682. int32_t adc_channel_request_conv(void *h, struct completion *conv_complete_evt)
  683. {
  684. struct msm_client_data *client = (struct msm_client_data *)h;
  685. struct msm_adc_platform_data *pdata =
  686. msm_adc_drv->pdev->dev.platform_data;
  687. struct msm_adc_channels *channel = &pdata->channel[client->adc_chan];
  688. struct adc_conv_slot *slot;
  689. int ret;
  690. if (conv_first_request) {
  691. ret = pm8058_xoadc_calib_device(channel->adc_dev_instance);
  692. if (ret) {
  693. pr_err("pmic8058 xoadc calibration failed, retry\n");
  694. return ret;
  695. }
  696. conv_first_request = false;
  697. }
  698. channel->adc_access_fn->adc_slot_request(channel->adc_dev_instance,
  699. &slot);
  700. if (slot) {
  701. atomic_inc(&msm_adc_drv->total_outst);
  702. mutex_lock(&client->lock);
  703. client->num_outstanding++;
  704. mutex_unlock(&client->lock);
  705. slot->conv.result.chan = client->adc_chan;
  706. slot->blocking = 0;
  707. slot->compk = conv_complete_evt;
  708. slot->client = client;
  709. slot->adc_request = START_OF_CONV;
  710. slot->chan_path = channel->chan_path_type;
  711. slot->chan_adc_config = channel->adc_config_type;
  712. slot->chan_adc_calib = channel->adc_calib_type;
  713. queue_work(msm_adc_drv->wq, &slot->work);
  714. return 0;
  715. }
  716. return -EBUSY;
  717. }
  718. int32_t adc_channel_read_result(void *h, struct adc_chan_result *chan_result)
  719. {
  720. struct msm_client_data *client = (struct msm_client_data *)h;
  721. struct msm_adc_platform_data *pdata =
  722. msm_adc_drv->pdev->dev.platform_data;
  723. struct msm_adc_channels *channel = pdata->channel;
  724. struct adc_conv_slot *slot;
  725. int rc = 0;
  726. mutex_lock(&client->lock);
  727. slot = list_first_entry(&client->complete_list,
  728. struct adc_conv_slot, list);
  729. if (!slot) {
  730. mutex_unlock(&client->lock);
  731. return -ENOMSG;
  732. }
  733. slot->client = NULL;
  734. list_del(&slot->list);
  735. client->num_complete--;
  736. mutex_unlock(&client->lock);
  737. *chan_result = slot->conv.result;
  738. /* restore this slot to reserve */
  739. channel[slot->conv.result.chan].adc_access_fn->adc_restore_slot(
  740. channel[slot->conv.result.chan].adc_dev_instance, slot);
  741. return rc;
  742. }
  743. static void msm_rpc_adc_conv_cb(void *context, u32 param,
  744. void *evt_buf, u32 len)
  745. {
  746. struct dal_adc_result *result = evt_buf;
  747. struct dal_conv_slot *slot = context;
  748. struct msm_adc_drv *msm_adc = msm_adc_drv;
  749. memcpy(&slot->result, result, sizeof(slot->result));
  750. /* for blocking requests, signal complete */
  751. if (slot->blocking)
  752. complete(&slot->comp);
  753. /* for non-blocking requests, add slot to the client completed list */
  754. else {
  755. struct msm_client_data *client = slot->client;
  756. mutex_lock(&client->lock);
  757. list_add(&slot->list, &client->complete_list);
  758. client->num_complete++;
  759. client->num_outstanding--;
  760. /*
  761. * if the client release has been invoked and this is call
  762. * corresponds to the last request, then signal release
  763. * to complete.
  764. */
  765. if (slot->client->online == 0 && client->num_outstanding == 0)
  766. wake_up_interruptible_all(&client->outst_wait);
  767. mutex_unlock(&client->lock);
  768. wake_up_interruptible_all(&client->data_wait);
  769. atomic_dec(&msm_adc->total_outst);
  770. /* verify driver remove has not been invoked */
  771. if (atomic_read(&msm_adc->online) == 0 &&
  772. atomic_read(&msm_adc->total_outst) == 0)
  773. wake_up_interruptible_all(&msm_adc->total_outst_wait);
  774. }
  775. }
  776. void msm_adc_conv_cb(void *context, u32 param,
  777. void *evt_buf, u32 len)
  778. {
  779. struct adc_conv_slot *slot = context;
  780. struct msm_adc_drv *msm_adc = msm_adc_drv;
  781. switch (slot->adc_request) {
  782. case START_OF_CONV:
  783. slot->adc_request = END_OF_CONV;
  784. break;
  785. case START_OF_CALIBRATION:
  786. slot->adc_request = END_OF_CALIBRATION;
  787. break;
  788. case END_OF_CALIBRATION:
  789. case END_OF_CONV:
  790. break;
  791. }
  792. queue_work(msm_adc->wq, &slot->work);
  793. }
  794. static void msm_adc_teardown_device_conv(struct platform_device *pdev,
  795. struct adc_dev *adc_dev)
  796. {
  797. struct dal_conv_state *conv_s = &adc_dev->conv;
  798. struct msm_adc_drv *msm_adc = platform_get_drvdata(pdev);
  799. struct dal_conv_slot *slot;
  800. int i;
  801. for (i = 0; i < MSM_ADC_DEV_MAX_INFLIGHT; i++) {
  802. slot = &conv_s->context[i];
  803. if (slot->cb_h) {
  804. dalrpc_dealloc_cb(msm_adc->dev_h, slot->cb_h);
  805. slot->cb_h = NULL;
  806. }
  807. }
  808. }
  809. static void msm_rpc_adc_teardown_device(struct platform_device *pdev,
  810. struct adc_dev *adc_dev)
  811. {
  812. struct dal_translation *transl = &adc_dev->transl;
  813. int i, num_chans = transl->hwmon_end - transl->hwmon_start + 1;
  814. if (adc_dev->sens_attr)
  815. for (i = 0; i < num_chans; i++)
  816. device_remove_file(&pdev->dev,
  817. &adc_dev->sens_attr[i].dev_attr);
  818. msm_adc_teardown_device_conv(pdev, adc_dev);
  819. kfree(adc_dev->fnames);
  820. kfree(adc_dev->sens_attr);
  821. kfree(adc_dev);
  822. }
  823. static void msm_rpc_adc_teardown_devices(struct platform_device *pdev)
  824. {
  825. struct msm_adc_platform_data *pdata = pdev->dev.platform_data;
  826. struct msm_adc_drv *msm_adc = platform_get_drvdata(pdev);
  827. int i, rc = 0;
  828. for (i = 0; i < pdata->num_adc; i++) {
  829. if (msm_adc->devs[i]) {
  830. msm_rpc_adc_teardown_device(pdev, msm_adc->devs[i]);
  831. msm_adc->devs[i] = NULL;
  832. } else
  833. break;
  834. }
  835. if (msm_adc->dev_h) {
  836. rc = daldevice_detach(msm_adc->dev_h);
  837. if (rc)
  838. dev_err(&pdev->dev, "Cannot detach from dal device\n");
  839. msm_adc->dev_h = NULL;
  840. }
  841. }
  842. static void msm_adc_teardown_device(struct platform_device *pdev,
  843. struct msm_adc_drv *msm_adc)
  844. {
  845. struct msm_adc_platform_data *pdata = pdev->dev.platform_data;
  846. int i, num_chans = pdata->num_chan_supported;
  847. if (pdata->num_chan_supported > 0) {
  848. if (msm_adc->sens_attr)
  849. for (i = 0; i < num_chans; i++)
  850. device_remove_file(&pdev->dev,
  851. &msm_adc->sens_attr[i].dev_attr);
  852. kfree(msm_adc->sens_attr);
  853. }
  854. }
  855. static void msm_adc_teardown(struct platform_device *pdev)
  856. {
  857. struct msm_adc_drv *msm_adc = platform_get_drvdata(pdev);
  858. if (!msm_adc)
  859. return;
  860. misc_deregister(&msm_adc->misc);
  861. if (msm_adc->hwmon)
  862. hwmon_device_unregister(msm_adc->hwmon);
  863. msm_rpc_adc_teardown_devices(pdev);
  864. msm_adc_teardown_device(pdev, msm_adc);
  865. kfree(msm_adc);
  866. platform_set_drvdata(pdev, NULL);
  867. }
  868. static int __devinit msm_adc_device_conv_init(struct msm_adc_drv *msm_adc,
  869. struct adc_dev *adc_dev)
  870. {
  871. struct platform_device *pdev = msm_adc->pdev;
  872. struct dal_conv_state *conv_s = &adc_dev->conv;
  873. struct dal_conv_slot *slot = conv_s->context;
  874. int rc, i;
  875. sema_init(&conv_s->slot_count, MSM_ADC_DEV_MAX_INFLIGHT);
  876. mutex_init(&conv_s->list_lock);
  877. INIT_LIST_HEAD(&conv_s->slots);
  878. for (i = 0; i < MSM_ADC_DEV_MAX_INFLIGHT; i++) {
  879. list_add(&slot->list, &conv_s->slots);
  880. slot->cb_h = dalrpc_alloc_cb(msm_adc->dev_h,
  881. msm_rpc_adc_conv_cb, slot);
  882. if (!slot->cb_h) {
  883. dev_err(&pdev->dev, "Unable to allocate DAL callback"
  884. " for slot %d\n", i);
  885. rc = -ENOMEM;
  886. goto dal_err_cb;
  887. }
  888. init_completion(&slot->comp);
  889. slot->idx = i;
  890. slot++;
  891. }
  892. return 0;
  893. dal_err_cb:
  894. msm_adc_teardown_device_conv(pdev, adc_dev);
  895. return rc;
  896. }
  897. static struct sensor_device_attribute msm_rpc_adc_curr_in_attr =
  898. SENSOR_ATTR(NULL, S_IRUGO, msm_adc_show_curr, NULL, 0);
  899. static int __devinit msm_rpc_adc_device_init_hwmon(struct platform_device *pdev,
  900. struct adc_dev *adc_dev)
  901. {
  902. struct dal_translation *transl = &adc_dev->transl;
  903. int i, rc, num_chans = transl->hwmon_end - transl->hwmon_start + 1;
  904. const char prefix[] = "curr", postfix[] = "_input";
  905. char tmpbuf[5];
  906. adc_dev->fnames = kzalloc(num_chans * MSM_ADC_MAX_FNAME +
  907. num_chans * sizeof(char *), GFP_KERNEL);
  908. if (!adc_dev->fnames) {
  909. dev_err(&pdev->dev, "Unable to allocate memory\n");
  910. return -ENOMEM;
  911. }
  912. adc_dev->sens_attr = kzalloc(num_chans *
  913. sizeof(struct sensor_device_attribute), GFP_KERNEL);
  914. if (!adc_dev->sens_attr) {
  915. dev_err(&pdev->dev, "Unable to allocate memory\n");
  916. rc = -ENOMEM;
  917. goto hwmon_err_fnames;
  918. }
  919. for (i = 0; i < num_chans; i++) {
  920. adc_dev->fnames[i] = (char *)adc_dev->fnames +
  921. i * MSM_ADC_MAX_FNAME + num_chans * sizeof(char *);
  922. strcpy(adc_dev->fnames[i], prefix);
  923. sprintf(tmpbuf, "%d", transl->hwmon_start + i);
  924. strcat(adc_dev->fnames[i], tmpbuf);
  925. strcat(adc_dev->fnames[i], postfix);
  926. msm_rpc_adc_curr_in_attr.index = transl->hwmon_start + i;
  927. msm_rpc_adc_curr_in_attr.dev_attr.attr.name =
  928. adc_dev->fnames[i];
  929. memcpy(&adc_dev->sens_attr[i], &msm_rpc_adc_curr_in_attr,
  930. sizeof(msm_rpc_adc_curr_in_attr));
  931. rc = device_create_file(&pdev->dev,
  932. &adc_dev->sens_attr[i].dev_attr);
  933. if (rc) {
  934. dev_err(&pdev->dev, "device_create_file failed for "
  935. "dal dev %u chan %d\n",
  936. adc_dev->transl.dal_dev_idx, i);
  937. goto hwmon_err_sens;
  938. }
  939. }
  940. return 0;
  941. hwmon_err_sens:
  942. kfree(adc_dev->sens_attr);
  943. hwmon_err_fnames:
  944. kfree(adc_dev->fnames);
  945. return rc;
  946. }
  947. static int __devinit msm_rpc_adc_device_init(struct platform_device *pdev)
  948. {
  949. struct msm_adc_platform_data *pdata = pdev->dev.platform_data;
  950. struct msm_adc_drv *msm_adc = platform_get_drvdata(pdev);
  951. struct adc_dev *adc_dev;
  952. struct adc_dev_spec target;
  953. int i, rc;
  954. int hwmon_cntr = pdata->num_chan_supported;
  955. for (i = 0; i < pdata->num_adc; i++) {
  956. adc_dev = kzalloc(sizeof(struct adc_dev), GFP_KERNEL);
  957. if (!adc_dev) {
  958. dev_err(&pdev->dev, "Unable to allocate memory\n");
  959. rc = -ENOMEM;
  960. goto dev_init_err;
  961. }
  962. msm_adc->devs[i] = adc_dev;
  963. adc_dev->name = pdata->dev_names[i];
  964. rc = msm_adc_device_conv_init(msm_adc, adc_dev);
  965. if (rc) {
  966. dev_err(&pdev->dev, "DAL device[%s] failed conv init\n",
  967. adc_dev->name);
  968. goto dev_init_err;
  969. }
  970. if (!pdata->target_hw == MSM_8x25) {
  971. /* DAL device lookup */
  972. rc = msm_adc_getinputproperties(msm_adc, adc_dev->name,
  973. &target);
  974. if (rc) {
  975. dev_err(&pdev->dev, "No such DAL device[%s]\n",
  976. adc_dev->name);
  977. goto dev_init_err;
  978. }
  979. adc_dev->transl.dal_dev_idx = target.dal.dev_idx;
  980. adc_dev->nchans = target.dal.chan_idx;
  981. } else {
  982. /* On targets prior to MSM7x30 the remote driver has
  983. only the channel list and no device id. */
  984. adc_dev->transl.dal_dev_idx = MSM_8x25_ADC_DEV_ID;
  985. adc_dev->nchans = MSM_8x25_CHAN_ID;
  986. }
  987. adc_dev->transl.hwmon_dev_idx = i;
  988. adc_dev->transl.hwmon_start = hwmon_cntr;
  989. adc_dev->transl.hwmon_end = hwmon_cntr + adc_dev->nchans - 1;
  990. hwmon_cntr += adc_dev->nchans;
  991. rc = msm_rpc_adc_device_init_hwmon(pdev, adc_dev);
  992. if (rc)
  993. goto dev_init_err;
  994. }
  995. return 0;
  996. dev_init_err:
  997. msm_rpc_adc_teardown_devices(pdev);
  998. return rc;
  999. }
  1000. static int __devinit msm_rpc_adc_init(struct platform_device *pdev1)
  1001. {
  1002. struct msm_adc_drv *msm_adc = msm_adc_drv;
  1003. struct platform_device *pdev = msm_adc->pdev;
  1004. struct msm_adc_platform_data *pdata = pdev->dev.platform_data;
  1005. int rc = 0;
  1006. dev_dbg(&pdev->dev, "msm_rpc_adc_init called\n");
  1007. if (!pdata) {
  1008. dev_err(&pdev->dev, "no platform data?\n");
  1009. return -EINVAL;
  1010. }
  1011. mutex_init(&msm_adc->prop_lock);
  1012. rc = daldevice_attach(MSM_ADC_DALRPC_DEVICEID,
  1013. MSM_ADC_DALRPC_PORT_NAME,
  1014. MSM_ADC_DALRPC_CPU,
  1015. &msm_adc->dev_h);
  1016. if (rc) {
  1017. dev_err(&pdev->dev, "Cannot attach to dal device\n");
  1018. return rc;
  1019. }
  1020. dev_dbg(&pdev->dev, "Attach to dal device Succeeded\n");
  1021. rc = msm_rpc_adc_device_init(pdev);
  1022. if (rc) {
  1023. dev_err(&pdev->dev, "msm_adc_dev_init failed\n");
  1024. goto err_cleanup;
  1025. }
  1026. init_waitqueue_head(&msm_adc->rpc_total_outst_wait);
  1027. atomic_set(&msm_adc->rpc_online, 1);
  1028. atomic_set(&msm_adc->rpc_total_outst, 0);
  1029. epm_init = true;
  1030. pr_info("msm_adc successfully registered\n");
  1031. return 0;
  1032. err_cleanup:
  1033. msm_rpc_adc_teardown_devices(pdev);
  1034. return rc;
  1035. }
  1036. /*
  1037. * Process the deferred job
  1038. */
  1039. void msm_adc_wq_work(struct work_struct *work)
  1040. {
  1041. struct adc_properties *adc_properties;
  1042. struct adc_conv_slot *slot = container_of(work,
  1043. struct adc_conv_slot, work);
  1044. uint32_t idx = slot->conv.result.chan;
  1045. struct msm_adc_platform_data *pdata =
  1046. msm_adc_drv->pdev->dev.platform_data;
  1047. struct msm_adc_channels *channel = &pdata->channel[idx];
  1048. int32_t adc_code;
  1049. switch (slot->adc_request) {
  1050. case START_OF_CONV:
  1051. channel->adc_access_fn->adc_select_chan_and_start_conv(
  1052. channel->adc_dev_instance, slot);
  1053. break;
  1054. case END_OF_CONV:
  1055. adc_properties = channel->adc_access_fn->adc_get_properties(
  1056. channel->adc_dev_instance);
  1057. if (channel->adc_access_fn->adc_read_adc_code)
  1058. channel->adc_access_fn->adc_read_adc_code(
  1059. channel->adc_dev_instance, &adc_code);
  1060. if (channel->chan_processor)
  1061. channel->chan_processor(adc_code, adc_properties,
  1062. &slot->chan_properties, &slot->conv.result);
  1063. /* Intentionally a fall thru here. Calibraton does not need
  1064. to perform channel processing, etc. However, both
  1065. end of conversion and end of calibration requires the below
  1066. fall thru code to be executed. */
  1067. case END_OF_CALIBRATION:
  1068. /* for blocking requests, signal complete */
  1069. if (slot->blocking)
  1070. complete(&slot->comp);
  1071. else {
  1072. struct msm_client_data *client = slot->client;
  1073. mutex_lock(&client->lock);
  1074. if (slot->adc_request == END_OF_CONV) {
  1075. list_add(&slot->list, &client->complete_list);
  1076. client->num_complete++;
  1077. }
  1078. client->num_outstanding--;
  1079. /*
  1080. * if the client release has been invoked and this is call
  1081. * corresponds to the last request, then signal release
  1082. * to complete.
  1083. */
  1084. if (slot->client->online == 0 &&
  1085. client->num_outstanding == 0)
  1086. wake_up_interruptible_all(&client->outst_wait);
  1087. mutex_unlock(&client->lock);
  1088. wake_up_interruptible_all(&client->data_wait);
  1089. atomic_dec(&msm_adc_drv->total_outst);
  1090. /* verify driver remove has not been invoked */
  1091. if (atomic_read(&msm_adc_drv->online) == 0 &&
  1092. atomic_read(&msm_adc_drv->total_outst) == 0)
  1093. wake_up_interruptible_all(
  1094. &msm_adc_drv->total_outst_wait);
  1095. if (slot->compk) /* Kernel space request */
  1096. complete(slot->compk);
  1097. if (slot->adc_request == END_OF_CALIBRATION)
  1098. channel->adc_access_fn->adc_restore_slot(
  1099. channel->adc_dev_instance, slot);
  1100. }
  1101. break;
  1102. case START_OF_CALIBRATION: /* code here to please code reviewers
  1103. to satisfy silly compiler warnings */
  1104. break;
  1105. }
  1106. }
  1107. static struct sensor_device_attribute msm_adc_curr_in_attr =
  1108. SENSOR_ATTR(NULL, S_IRUGO, msm_adc_show_curr, NULL, 0);
  1109. static int __devinit msm_adc_init_hwmon(struct platform_device *pdev,
  1110. struct msm_adc_drv *msm_adc)
  1111. {
  1112. struct msm_adc_platform_data *pdata = pdev->dev.platform_data;
  1113. struct msm_adc_channels *channel = pdata->channel;
  1114. int i, rc, num_chans = pdata->num_chan_supported;
  1115. if (!channel)
  1116. return -EINVAL;
  1117. msm_adc->sens_attr = kzalloc(num_chans *
  1118. sizeof(struct sensor_device_attribute), GFP_KERNEL);
  1119. if (!msm_adc->sens_attr) {
  1120. dev_err(&pdev->dev, "Unable to allocate memory\n");
  1121. rc = -ENOMEM;
  1122. goto hwmon_err_sens;
  1123. }
  1124. for (i = 0; i < num_chans; i++) {
  1125. msm_adc_curr_in_attr.index = i;
  1126. msm_adc_curr_in_attr.dev_attr.attr.name = channel[i].name;
  1127. memcpy(&msm_adc->sens_attr[i], &msm_adc_curr_in_attr,
  1128. sizeof(msm_adc_curr_in_attr));
  1129. rc = device_create_file(&pdev->dev,
  1130. &msm_adc->sens_attr[i].dev_attr);
  1131. if (rc) {
  1132. dev_err(&pdev->dev, "device_create_file failed for "
  1133. "dal dev %s\n",
  1134. channel[i].name);
  1135. goto hwmon_err_sens;
  1136. }
  1137. }
  1138. return 0;
  1139. hwmon_err_sens:
  1140. kfree(msm_adc->sens_attr);
  1141. return rc;
  1142. }
  1143. static struct platform_driver msm_adc_rpcrouter_remote_driver = {
  1144. .probe = msm_rpc_adc_init,
  1145. .driver = {
  1146. .name = MSM_ADC_DALRPC_PORT_NAME,
  1147. .owner = THIS_MODULE,
  1148. },
  1149. };
  1150. static int __devinit msm_adc_probe(struct platform_device *pdev)
  1151. {
  1152. struct msm_adc_platform_data *pdata = pdev->dev.platform_data;
  1153. struct msm_adc_drv *msm_adc;
  1154. int rc = 0;
  1155. if (!pdata) {
  1156. dev_err(&pdev->dev, "no platform data?\n");
  1157. return -EINVAL;
  1158. }
  1159. msm_adc = kzalloc(sizeof(struct msm_adc_drv), GFP_KERNEL);
  1160. if (!msm_adc) {
  1161. dev_err(&pdev->dev, "Unable to allocate memory\n");
  1162. return -ENOMEM;
  1163. }
  1164. platform_set_drvdata(pdev, msm_adc);
  1165. msm_adc_drv = msm_adc;
  1166. msm_adc->pdev = pdev;
  1167. if (pdata->target_hw == MSM_8x60 || pdata->target_hw == FSM_9xxx) {
  1168. rc = msm_adc_init_hwmon(pdev, msm_adc);
  1169. if (rc) {
  1170. dev_err(&pdev->dev, "msm_adc_dev_init failed\n");
  1171. goto err_cleanup;
  1172. }
  1173. }
  1174. msm_adc->hwmon = hwmon_device_register(&pdev->dev);
  1175. if (IS_ERR(msm_adc->hwmon)) {
  1176. dev_err(&pdev->dev, "hwmon_device_register failed\n");
  1177. rc = PTR_ERR(msm_adc->hwmon);
  1178. goto err_cleanup;
  1179. }
  1180. msm_adc->misc.name = MSM_ADC_DRIVER_NAME;
  1181. msm_adc->misc.minor = MISC_DYNAMIC_MINOR;
  1182. msm_adc->misc.fops = &msm_adc_fops;
  1183. if (misc_register(&msm_adc->misc)) {
  1184. dev_err(&pdev->dev, "Unable to register misc device!\n");
  1185. goto err_cleanup;
  1186. }
  1187. init_waitqueue_head(&msm_adc->total_outst_wait);
  1188. atomic_set(&msm_adc->online, 1);
  1189. atomic_set(&msm_adc->total_outst, 0);
  1190. msm_adc->wq = create_singlethread_workqueue("msm_adc");
  1191. if (!msm_adc->wq)
  1192. goto err_cleanup;
  1193. if (pdata->num_adc > 0) {
  1194. if (pdata->target_hw == MSM_8x60)
  1195. platform_driver_register(
  1196. &msm_adc_rpcrouter_remote_driver);
  1197. else
  1198. msm_rpc_adc_init(pdev);
  1199. }
  1200. conv_first_request = true;
  1201. pr_info("msm_adc successfully registered\n");
  1202. return 0;
  1203. err_cleanup:
  1204. msm_adc_teardown(pdev);
  1205. return rc;
  1206. }
  1207. static int __devexit msm_adc_remove(struct platform_device *pdev)
  1208. {
  1209. int rc;
  1210. struct msm_adc_drv *msm_adc = platform_get_drvdata(pdev);
  1211. atomic_set(&msm_adc->online, 0);
  1212. atomic_set(&msm_adc->rpc_online, 0);
  1213. misc_deregister(&msm_adc->misc);
  1214. hwmon_device_unregister(msm_adc->hwmon);
  1215. msm_adc->hwmon = NULL;
  1216. /*
  1217. * We may still have outstanding transactions in flight that have not
  1218. * completed. Make sure they're completed before tearing down.
  1219. */
  1220. rc = wait_event_interruptible(msm_adc->total_outst_wait,
  1221. atomic_read(&msm_adc->total_outst) == 0);
  1222. if (rc) {
  1223. pr_err("%s: wait_event_interruptible failed rc = %d\n",
  1224. __func__, rc);
  1225. return rc;
  1226. }
  1227. rc = wait_event_interruptible(msm_adc->rpc_total_outst_wait,
  1228. atomic_read(&msm_adc->rpc_total_outst) == 0);
  1229. if (rc) {
  1230. pr_err("%s: wait_event_interruptible failed rc = %d\n",
  1231. __func__, rc);
  1232. return rc;
  1233. }
  1234. msm_adc_teardown(pdev);
  1235. pr_info("msm_adc unregistered\n");
  1236. return 0;
  1237. }
  1238. static struct platform_driver msm_adc_driver = {
  1239. .probe = msm_adc_probe,
  1240. .remove = __devexit_p(msm_adc_remove),
  1241. .driver = {
  1242. .name = MSM_ADC_DRIVER_NAME,
  1243. .owner = THIS_MODULE,
  1244. },
  1245. };
  1246. static int __init msm_adc_init(void)
  1247. {
  1248. return platform_driver_register(&msm_adc_driver);
  1249. }
  1250. module_init(msm_adc_init);
  1251. static void __exit msm_adc_exit(void)
  1252. {
  1253. platform_driver_unregister(&msm_adc_driver);
  1254. }
  1255. module_exit(msm_adc_exit);
  1256. MODULE_DESCRIPTION("MSM ADC Driver");
  1257. MODULE_ALIAS("platform:msm_adc");
  1258. MODULE_LICENSE("GPL v2");
  1259. MODULE_VERSION("0.1");