msm_adc.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #ifndef __MSM_ADC_H
  2. #define __MSM_ADC_H
  3. #include <linux/sched.h>
  4. #define MSM_ADC_MAX_CHAN_STR 64
  5. /* must be <= to the max buffer size in the modem implementation */
  6. #define MSM_ADC_DEV_MAX_INFLIGHT 9
  7. #define MSM_ADC_IOCTL_CODE 0x90
  8. struct msm_adc_conversion {
  9. /* hwmon channel number - this is not equivalent to the DAL chan */
  10. uint32_t chan;
  11. /* returned result in ms */
  12. int result;
  13. };
  14. struct adc_chan_result {
  15. /* The channel number of the requesting/requested conversion */
  16. uint32_t chan;
  17. /* The pre-calibrated digital output of a given ADC relative to the
  18. ADC reference */
  19. int32_t adc_code;
  20. /* in units specific for a given ADC; most ADC uses reference voltage
  21. * but some ADC uses reference current. This measurement here is
  22. * a number relative to a reference of a given ADC */
  23. int64_t measurement;
  24. /* The data meaningful for each individual channel whether it is
  25. * voltage, current, temperature, etc. */
  26. int64_t physical;
  27. };
  28. /*
  29. * Issue a blocking adc conversion request. Once the call returns, the data
  30. * can be found in the 'physical' field of adc_chan_result. This call will
  31. * return ENODATA if there is an invalid result returned by the modem driver.
  32. */
  33. #define MSM_ADC_REQUEST _IOWR(MSM_ADC_IOCTL_CODE, 1, \
  34. struct adc_chan_result)
  35. /*
  36. * Issue a non-blocking adc conversion request. The results from this
  37. * request can be obtained by calling AIO_READ once the transfer is
  38. * completed. To verify completion, the blocking call AIO_POLL can be used.
  39. * If there are no slot resources, this call will return an error with errno
  40. * set to EWOULDBLOCK.
  41. */
  42. #define MSM_ADC_AIO_REQUEST _IOWR(MSM_ADC_IOCTL_CODE, 2, \
  43. struct adc_chan_result)
  44. /*
  45. * Same non-blocking semantics as AIO_REQUEST, except this call will block
  46. * if there are no available slot resources. This call can fail with errno
  47. * set to EDEADLK if there are no resources and the file descriptor in question
  48. * has outstanding conversion requests already. This is done so the client
  49. * does not block on resources that can only be freed by reading the results --
  50. * effectively deadlocking the system. In this case, the client must read
  51. * pending results before proceeding to free up resources.
  52. */
  53. #define MSM_ADC_AIO_REQUEST_BLOCK_RES _IOWR(MSM_ADC_IOCTL_CODE, 3, \
  54. struct adc_chan_result)
  55. /*
  56. * Returns the number of pending results that are associated with a particular
  57. * file descriptor. If there are no pending results, this call will block until
  58. * there is at least one. If there are no requests queued at all on this file
  59. * descriptor, this call will fail with EDEADLK. This is to prevent deadlock in
  60. * a single-threaded scenario where POLL would never return.
  61. */
  62. #define MSM_ADC_AIO_POLL _IOR(MSM_ADC_IOCTL_CODE, 4, \
  63. uint32_t)
  64. #define MSM_ADC_FLUID_INIT _IOR(MSM_ADC_IOCTL_CODE, 5, \
  65. uint32_t)
  66. #define MSM_ADC_FLUID_DEINIT _IOR(MSM_ADC_IOCTL_CODE, 6, \
  67. uint32_t)
  68. struct msm_adc_aio_result {
  69. uint32_t chan;
  70. int result;
  71. };
  72. /*
  73. * Read the results from an AIO / non-blocking conversion request. AIO_POLL
  74. * should be used before using this command to verify how many pending requests
  75. * are available for the file descriptor. This call will fail with errno set to
  76. * ENOMSG if there are no pending messages to be read at the time of the call.
  77. * The call will return ENODATA if there is an invalid result returned by the
  78. * modem driver.
  79. */
  80. #define MSM_ADC_AIO_READ _IOR(MSM_ADC_IOCTL_CODE, 5, \
  81. struct adc_chan_result)
  82. struct msm_adc_lookup {
  83. /* channel name (input) */
  84. char name[MSM_ADC_MAX_CHAN_STR];
  85. /* local channel index (output) */
  86. uint32_t chan_idx;
  87. };
  88. /*
  89. * Look up a channel name and get back an index that can be used
  90. * as a parameter to the conversion request commands.
  91. */
  92. #define MSM_ADC_LOOKUP _IOWR(MSM_ADC_IOCTL_CODE, 6, \
  93. struct msm_adc_lookup)
  94. #ifdef __KERNEL__
  95. #define MSM_ADC_MAX_NUM_DEVS 3
  96. enum {
  97. ADC_CONFIG_TYPE1,
  98. ADC_CONFIG_TYPE2,
  99. ADC_CONFIG_NONE = 0xffffffff
  100. };
  101. enum {
  102. ADC_CALIB_CONFIG_TYPE1,
  103. ADC_CALIB_CONFIG_TYPE2,
  104. ADC_CALIB_CONFIG_TYPE3,
  105. ADC_CALIB_CONFIG_TYPE4,
  106. ADC_CALIB_CONFIG_TYPE5,
  107. ADC_CALIB_CONFIG_TYPE6,
  108. ADC_CALIB_CONFIG_TYPE7,
  109. ADC_CALIB_CONFIG_NONE = 0xffffffff
  110. };
  111. enum {
  112. /* CHAN_PATH_TYPEn is specific for each ADC driver
  113. and can be used however way it wants*/
  114. CHAN_PATH_TYPE1,
  115. CHAN_PATH_TYPE2,
  116. CHAN_PATH_TYPE3,
  117. CHAN_PATH_TYPE4,
  118. CHAN_PATH_TYPE5,
  119. CHAN_PATH_TYPE6,
  120. CHAN_PATH_TYPE7,
  121. CHAN_PATH_TYPE8,
  122. CHAN_PATH_TYPE9,
  123. CHAN_PATH_TYPE10,
  124. CHAN_PATH_TYPE11,
  125. CHAN_PATH_TYPE12,
  126. CHAN_PATH_TYPE13,
  127. CHAN_PATH_TYPE14,
  128. CHAN_PATH_TYPE15,
  129. CHAN_PATH_TYPE16,
  130. /* A given channel connects directly to the ADC */
  131. CHAN_PATH_TYPE_NONE = 0xffffffff
  132. };
  133. #define CHANNEL_ADC_BATT_ID 0
  134. #define CHANNEL_ADC_BATT_THERM 1
  135. #define CHANNEL_ADC_BATT_AMON 2
  136. #define CHANNEL_ADC_VBATT 3
  137. #define CHANNEL_ADC_VCOIN 4
  138. #define CHANNEL_ADC_VCHG 5
  139. #define CHANNEL_ADC_CHG_MONITOR 6
  140. #define CHANNEL_ADC_VPH_PWR 7
  141. #define CHANNEL_ADC_USB_VBUS 8
  142. #define CHANNEL_ADC_DIE_TEMP 9
  143. #define CHANNEL_ADC_DIE_TEMP_4K 0xa
  144. #define CHANNEL_ADC_XOTHERM 0xb
  145. #define CHANNEL_ADC_XOTHERM_4K 0xc
  146. #define CHANNEL_ADC_HDSET 0xd
  147. #define CHANNEL_ADC_MSM_THERM 0xe
  148. #define CHANNEL_ADC_625_REF 0xf
  149. #define CHANNEL_ADC_1250_REF 0x10
  150. #define CHANNEL_ADC_325_REF 0x11
  151. #define CHANNEL_ADC_FSM_THERM 0x12
  152. #define CHANNEL_ADC_PA_THERM 0x13
  153. enum {
  154. CALIB_STARTED,
  155. CALIB_NOT_REQUIRED = 0xffffffff,
  156. };
  157. struct linear_graph {
  158. int32_t offset;
  159. int32_t dy; /* Slope numerator */
  160. int32_t dx; /* Slope denominator */
  161. };
  162. struct adc_map_pt {
  163. int32_t x;
  164. int32_t y;
  165. };
  166. struct adc_properties {
  167. uint32_t adc_reference; /* milli-voltage for this adc */
  168. uint32_t bitresolution;
  169. bool bipolar;
  170. uint32_t conversiontime;
  171. };
  172. struct chan_properties {
  173. uint32_t gain_numerator;
  174. uint32_t gain_denominator;
  175. struct linear_graph *adc_graph;
  176. /* this maybe the same as adc_properties.ConversionTime
  177. if channel does not change the adc properties */
  178. uint32_t chan_conv_time;
  179. };
  180. struct msm_adc_channels {
  181. char *name;
  182. uint32_t channel_name;
  183. uint32_t adc_dev_instance;
  184. struct adc_access_fn *adc_access_fn;
  185. uint32_t chan_path_type;
  186. uint32_t adc_config_type;
  187. uint32_t adc_calib_type;
  188. int32_t (*chan_processor)(int32_t, const struct adc_properties *,
  189. const struct chan_properties *, struct adc_chan_result *);
  190. };
  191. struct msm_adc_platform_data {
  192. struct msm_adc_channels *channel;
  193. uint32_t num_chan_supported;
  194. uint32_t num_adc;
  195. uint32_t chan_per_adc;
  196. char **dev_names;
  197. uint32_t target_hw;
  198. uint32_t gpio_config;
  199. u32 (*adc_gpio_enable) (int);
  200. u32 (*adc_gpio_disable) (int);
  201. u32 (*adc_fluid_enable) (void);
  202. u32 (*adc_fluid_disable) (void);
  203. };
  204. enum hw_type {
  205. MSM_7x30,
  206. MSM_8x60,
  207. FSM_9xxx,
  208. MSM_8x25,
  209. };
  210. enum epm_gpio_config {
  211. MPROC_CONFIG,
  212. APROC_CONFIG
  213. };
  214. enum adc_request {
  215. START_OF_CONV,
  216. END_OF_CONV,
  217. START_OF_CALIBRATION,
  218. END_OF_CALIBRATION,
  219. };
  220. struct adc_dev_spec {
  221. uint32_t hwmon_dev_idx;
  222. struct dal_dev_spec {
  223. uint32_t dev_idx;
  224. uint32_t chan_idx;
  225. } dal;
  226. };
  227. struct dal_conv_request {
  228. struct dal_dev_spec target;
  229. void *cb_h;
  230. };
  231. struct dal_adc_result {
  232. uint32_t status;
  233. uint32_t token;
  234. uint32_t dev_idx;
  235. uint32_t chan_idx;
  236. int physical;
  237. uint32_t percent;
  238. uint32_t microvolts;
  239. uint32_t reserved;
  240. };
  241. struct dal_conv_slot {
  242. void *cb_h;
  243. struct dal_adc_result result;
  244. struct completion comp;
  245. struct list_head list;
  246. uint32_t idx;
  247. uint32_t chan_idx;
  248. bool blocking;
  249. struct msm_client_data *client;
  250. };
  251. struct dal_translation {
  252. uint32_t dal_dev_idx;
  253. uint32_t hwmon_dev_idx;
  254. uint32_t hwmon_start;
  255. uint32_t hwmon_end;
  256. };
  257. struct msm_client_data {
  258. struct list_head complete_list;
  259. bool online;
  260. int32_t adc_chan;
  261. uint32_t num_complete;
  262. uint32_t num_outstanding;
  263. wait_queue_head_t data_wait;
  264. wait_queue_head_t outst_wait;
  265. struct mutex lock;
  266. };
  267. struct adc_conv_slot {
  268. void *cb_h;
  269. union {
  270. struct adc_chan_result result;
  271. struct dal_adc_result dal_result;
  272. } conv;
  273. struct completion comp;
  274. struct completion *compk;
  275. struct list_head list;
  276. uint32_t idx;
  277. enum adc_request adc_request;
  278. bool blocking;
  279. struct msm_client_data *client;
  280. struct work_struct work;
  281. struct chan_properties chan_properties;
  282. uint32_t chan_path;
  283. uint32_t chan_adc_config;
  284. uint32_t chan_adc_calib;
  285. };
  286. struct adc_access_fn {
  287. int32_t (*adc_select_chan_and_start_conv)(uint32_t,
  288. struct adc_conv_slot*);
  289. int32_t (*adc_read_adc_code)(uint32_t dev_instance, int32_t *data);
  290. struct adc_properties *(*adc_get_properties)(uint32_t dev_instance);
  291. void (*adc_slot_request)(uint32_t dev_instance,
  292. struct adc_conv_slot **);
  293. void (*adc_restore_slot)(uint32_t dev_instance,
  294. struct adc_conv_slot *slot);
  295. int32_t (*adc_calibrate)(uint32_t dev_instance, struct adc_conv_slot*,
  296. int *);
  297. };
  298. void msm_adc_wq_work(struct work_struct *work);
  299. void msm_adc_conv_cb(void *context, u32 param, void *evt_buf, u32 len);
  300. #ifdef CONFIG_SENSORS_MSM_ADC
  301. int32_t adc_channel_open(uint32_t channel, void **h);
  302. int32_t adc_channel_close(void *h);
  303. int32_t adc_channel_request_conv(void *h, struct completion *conv_complete_evt);
  304. int32_t adc_channel_read_result(void *h, struct adc_chan_result *chan_result);
  305. #else
  306. static inline int32_t adc_channel_open(uint32_t channel, void **h)
  307. {
  308. pr_err("%s.not supported.\n", __func__);
  309. return -ENODEV;
  310. }
  311. static inline int32_t adc_channel_close(void *h)
  312. {
  313. pr_err("%s.not supported.\n", __func__);
  314. return -ENODEV;
  315. }
  316. static inline int32_t
  317. adc_channel_request_conv(void *h, struct completion *conv_complete_evt)
  318. {
  319. pr_err("%s.not supported.\n", __func__);
  320. return -ENODEV;
  321. }
  322. static inline int32_t
  323. adc_channel_read_result(void *h, struct adc_chan_result *chan_result)
  324. {
  325. pr_err("%s.not supported.\n", __func__);
  326. return -ENODEV;
  327. }
  328. #endif /* CONFIG_SENSORS_MSM_ADC */
  329. #endif
  330. #endif /* __MSM_ADC_H */