hid-sensor-als.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2012, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/hid-sensor-hub.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/buffer.h>
  30. #include <linux/iio/trigger_consumer.h>
  31. #include <linux/iio/triggered_buffer.h>
  32. #include "../common/hid-sensors/hid-sensor-trigger.h"
  33. #define CHANNEL_SCAN_INDEX_ILLUM 0
  34. struct als_state {
  35. struct hid_sensor_hub_callbacks callbacks;
  36. struct hid_sensor_common common_attributes;
  37. struct hid_sensor_hub_attribute_info als_illum;
  38. u32 illum;
  39. int scale_pre_decml;
  40. int scale_post_decml;
  41. int scale_precision;
  42. int value_offset;
  43. };
  44. /* Channel definitions */
  45. static const struct iio_chan_spec als_channels[] = {
  46. {
  47. .type = IIO_INTENSITY,
  48. .modified = 1,
  49. .channel2 = IIO_MOD_LIGHT_BOTH,
  50. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  51. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  52. BIT(IIO_CHAN_INFO_SCALE) |
  53. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  54. BIT(IIO_CHAN_INFO_HYSTERESIS),
  55. .scan_index = CHANNEL_SCAN_INDEX_ILLUM,
  56. }
  57. };
  58. /* Adjust channel real bits based on report descriptor */
  59. static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  60. int channel, int size)
  61. {
  62. channels[channel].scan_type.sign = 's';
  63. /* Real storage bits will change based on the report desc. */
  64. channels[channel].scan_type.realbits = size * 8;
  65. /* Maximum size of a sample to capture is u32 */
  66. channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  67. }
  68. /* Channel read_raw handler */
  69. static int als_read_raw(struct iio_dev *indio_dev,
  70. struct iio_chan_spec const *chan,
  71. int *val, int *val2,
  72. long mask)
  73. {
  74. struct als_state *als_state = iio_priv(indio_dev);
  75. int report_id = -1;
  76. u32 address;
  77. int ret_type;
  78. s32 poll_value;
  79. *val = 0;
  80. *val2 = 0;
  81. switch (mask) {
  82. case 0:
  83. switch (chan->scan_index) {
  84. case CHANNEL_SCAN_INDEX_ILLUM:
  85. report_id = als_state->als_illum.report_id;
  86. address =
  87. HID_USAGE_SENSOR_LIGHT_ILLUM;
  88. break;
  89. default:
  90. report_id = -1;
  91. break;
  92. }
  93. if (report_id >= 0) {
  94. poll_value = hid_sensor_read_poll_value(
  95. &als_state->common_attributes);
  96. if (poll_value < 0)
  97. return -EINVAL;
  98. hid_sensor_power_state(&als_state->common_attributes,
  99. true);
  100. msleep_interruptible(poll_value * 2);
  101. *val = sensor_hub_input_attr_get_raw_value(
  102. als_state->common_attributes.hsdev,
  103. HID_USAGE_SENSOR_ALS, address,
  104. report_id,
  105. SENSOR_HUB_SYNC);
  106. hid_sensor_power_state(&als_state->common_attributes,
  107. false);
  108. } else {
  109. *val = 0;
  110. return -EINVAL;
  111. }
  112. ret_type = IIO_VAL_INT;
  113. break;
  114. case IIO_CHAN_INFO_SCALE:
  115. *val = als_state->scale_pre_decml;
  116. *val2 = als_state->scale_post_decml;
  117. ret_type = als_state->scale_precision;
  118. break;
  119. case IIO_CHAN_INFO_OFFSET:
  120. *val = als_state->value_offset;
  121. ret_type = IIO_VAL_INT;
  122. break;
  123. case IIO_CHAN_INFO_SAMP_FREQ:
  124. ret_type = hid_sensor_read_samp_freq_value(
  125. &als_state->common_attributes, val, val2);
  126. break;
  127. case IIO_CHAN_INFO_HYSTERESIS:
  128. ret_type = hid_sensor_read_raw_hyst_value(
  129. &als_state->common_attributes, val, val2);
  130. break;
  131. default:
  132. ret_type = -EINVAL;
  133. break;
  134. }
  135. return ret_type;
  136. }
  137. /* Channel write_raw handler */
  138. static int als_write_raw(struct iio_dev *indio_dev,
  139. struct iio_chan_spec const *chan,
  140. int val,
  141. int val2,
  142. long mask)
  143. {
  144. struct als_state *als_state = iio_priv(indio_dev);
  145. int ret = 0;
  146. switch (mask) {
  147. case IIO_CHAN_INFO_SAMP_FREQ:
  148. ret = hid_sensor_write_samp_freq_value(
  149. &als_state->common_attributes, val, val2);
  150. break;
  151. case IIO_CHAN_INFO_HYSTERESIS:
  152. ret = hid_sensor_write_raw_hyst_value(
  153. &als_state->common_attributes, val, val2);
  154. break;
  155. default:
  156. ret = -EINVAL;
  157. }
  158. return ret;
  159. }
  160. static const struct iio_info als_info = {
  161. .driver_module = THIS_MODULE,
  162. .read_raw = &als_read_raw,
  163. .write_raw = &als_write_raw,
  164. };
  165. /* Function to push data to buffer */
  166. static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
  167. int len)
  168. {
  169. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  170. iio_push_to_buffers(indio_dev, data);
  171. }
  172. /* Callback handler to send event after all samples are received and captured */
  173. static int als_proc_event(struct hid_sensor_hub_device *hsdev,
  174. unsigned usage_id,
  175. void *priv)
  176. {
  177. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  178. struct als_state *als_state = iio_priv(indio_dev);
  179. dev_dbg(&indio_dev->dev, "als_proc_event\n");
  180. if (atomic_read(&als_state->common_attributes.data_ready))
  181. hid_sensor_push_data(indio_dev,
  182. &als_state->illum,
  183. sizeof(als_state->illum));
  184. return 0;
  185. }
  186. /* Capture samples in local storage */
  187. static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
  188. unsigned usage_id,
  189. size_t raw_len, char *raw_data,
  190. void *priv)
  191. {
  192. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  193. struct als_state *als_state = iio_priv(indio_dev);
  194. int ret = -EINVAL;
  195. switch (usage_id) {
  196. case HID_USAGE_SENSOR_LIGHT_ILLUM:
  197. als_state->illum = *(u32 *)raw_data;
  198. ret = 0;
  199. break;
  200. default:
  201. break;
  202. }
  203. return ret;
  204. }
  205. /* Parse report which is specific to an usage id*/
  206. static int als_parse_report(struct platform_device *pdev,
  207. struct hid_sensor_hub_device *hsdev,
  208. struct iio_chan_spec *channels,
  209. unsigned usage_id,
  210. struct als_state *st)
  211. {
  212. int ret;
  213. ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
  214. usage_id,
  215. HID_USAGE_SENSOR_LIGHT_ILLUM,
  216. &st->als_illum);
  217. if (ret < 0)
  218. return ret;
  219. als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
  220. st->als_illum.size);
  221. dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
  222. st->als_illum.report_id);
  223. st->scale_precision = hid_sensor_format_scale(
  224. HID_USAGE_SENSOR_ALS,
  225. &st->als_illum,
  226. &st->scale_pre_decml, &st->scale_post_decml);
  227. /* Set Sensitivity field ids, when there is no individual modifier */
  228. if (st->common_attributes.sensitivity.index < 0) {
  229. sensor_hub_input_get_attribute_info(hsdev,
  230. HID_FEATURE_REPORT, usage_id,
  231. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  232. HID_USAGE_SENSOR_DATA_LIGHT,
  233. &st->common_attributes.sensitivity);
  234. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  235. st->common_attributes.sensitivity.index,
  236. st->common_attributes.sensitivity.report_id);
  237. }
  238. return ret;
  239. }
  240. /* Function to initialize the processing for usage id */
  241. static int hid_als_probe(struct platform_device *pdev)
  242. {
  243. int ret = 0;
  244. static const char *name = "als";
  245. struct iio_dev *indio_dev;
  246. struct als_state *als_state;
  247. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  248. struct iio_chan_spec *channels;
  249. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
  250. if (!indio_dev)
  251. return -ENOMEM;
  252. platform_set_drvdata(pdev, indio_dev);
  253. als_state = iio_priv(indio_dev);
  254. als_state->common_attributes.hsdev = hsdev;
  255. als_state->common_attributes.pdev = pdev;
  256. ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
  257. &als_state->common_attributes);
  258. if (ret) {
  259. dev_err(&pdev->dev, "failed to setup common attributes\n");
  260. return ret;
  261. }
  262. channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL);
  263. if (!channels) {
  264. dev_err(&pdev->dev, "failed to duplicate channels\n");
  265. return -ENOMEM;
  266. }
  267. ret = als_parse_report(pdev, hsdev, channels,
  268. HID_USAGE_SENSOR_ALS, als_state);
  269. if (ret) {
  270. dev_err(&pdev->dev, "failed to setup attributes\n");
  271. goto error_free_dev_mem;
  272. }
  273. indio_dev->channels = channels;
  274. indio_dev->num_channels =
  275. ARRAY_SIZE(als_channels);
  276. indio_dev->dev.parent = &pdev->dev;
  277. indio_dev->info = &als_info;
  278. indio_dev->name = name;
  279. indio_dev->modes = INDIO_DIRECT_MODE;
  280. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  281. NULL, NULL);
  282. if (ret) {
  283. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  284. goto error_free_dev_mem;
  285. }
  286. atomic_set(&als_state->common_attributes.data_ready, 0);
  287. ret = hid_sensor_setup_trigger(indio_dev, name,
  288. &als_state->common_attributes);
  289. if (ret < 0) {
  290. dev_err(&pdev->dev, "trigger setup failed\n");
  291. goto error_unreg_buffer_funcs;
  292. }
  293. ret = iio_device_register(indio_dev);
  294. if (ret) {
  295. dev_err(&pdev->dev, "device register failed\n");
  296. goto error_remove_trigger;
  297. }
  298. als_state->callbacks.send_event = als_proc_event;
  299. als_state->callbacks.capture_sample = als_capture_sample;
  300. als_state->callbacks.pdev = pdev;
  301. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
  302. &als_state->callbacks);
  303. if (ret < 0) {
  304. dev_err(&pdev->dev, "callback reg failed\n");
  305. goto error_iio_unreg;
  306. }
  307. return ret;
  308. error_iio_unreg:
  309. iio_device_unregister(indio_dev);
  310. error_remove_trigger:
  311. hid_sensor_remove_trigger(&als_state->common_attributes);
  312. error_unreg_buffer_funcs:
  313. iio_triggered_buffer_cleanup(indio_dev);
  314. error_free_dev_mem:
  315. kfree(indio_dev->channels);
  316. return ret;
  317. }
  318. /* Function to deinitialize the processing for usage id */
  319. static int hid_als_remove(struct platform_device *pdev)
  320. {
  321. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  322. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  323. struct als_state *als_state = iio_priv(indio_dev);
  324. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
  325. iio_device_unregister(indio_dev);
  326. hid_sensor_remove_trigger(&als_state->common_attributes);
  327. iio_triggered_buffer_cleanup(indio_dev);
  328. kfree(indio_dev->channels);
  329. return 0;
  330. }
  331. static struct platform_device_id hid_als_ids[] = {
  332. {
  333. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  334. .name = "HID-SENSOR-200041",
  335. },
  336. { /* sentinel */ }
  337. };
  338. MODULE_DEVICE_TABLE(platform, hid_als_ids);
  339. static struct platform_driver hid_als_platform_driver = {
  340. .id_table = hid_als_ids,
  341. .driver = {
  342. .name = KBUILD_MODNAME,
  343. },
  344. .probe = hid_als_probe,
  345. .remove = hid_als_remove,
  346. };
  347. module_platform_driver(hid_als_platform_driver);
  348. MODULE_DESCRIPTION("HID Sensor ALS");
  349. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  350. MODULE_LICENSE("GPL");