hid-sensor-als.c 11 KB

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