hid-sensor-als.c 9.5 KB

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