hid-sensor-als.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 int als_write_raw_get_fmt(struct iio_dev *indio_dev,
  147. struct iio_chan_spec const *chan,
  148. long mask)
  149. {
  150. return IIO_VAL_INT_PLUS_MICRO;
  151. }
  152. static const struct iio_info als_info = {
  153. .driver_module = THIS_MODULE,
  154. .read_raw = &als_read_raw,
  155. .write_raw = &als_write_raw,
  156. .write_raw_get_fmt = &als_write_raw_get_fmt,
  157. };
  158. /* Function to push data to buffer */
  159. static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
  160. {
  161. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  162. iio_push_to_buffers(indio_dev, (u8 *)data);
  163. }
  164. /* Callback handler to send event after all samples are received and captured */
  165. static int als_proc_event(struct hid_sensor_hub_device *hsdev,
  166. unsigned usage_id,
  167. void *priv)
  168. {
  169. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  170. struct als_state *als_state = iio_priv(indio_dev);
  171. dev_dbg(&indio_dev->dev, "als_proc_event [%d]\n",
  172. als_state->common_attributes.data_ready);
  173. if (als_state->common_attributes.data_ready)
  174. hid_sensor_push_data(indio_dev,
  175. (u8 *)&als_state->illum,
  176. sizeof(als_state->illum));
  177. return 0;
  178. }
  179. /* Capture samples in local storage */
  180. static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
  181. unsigned usage_id,
  182. size_t raw_len, char *raw_data,
  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. int ret = -EINVAL;
  188. switch (usage_id) {
  189. case HID_USAGE_SENSOR_LIGHT_ILLUM:
  190. als_state->illum = *(u32 *)raw_data;
  191. ret = 0;
  192. break;
  193. default:
  194. break;
  195. }
  196. return ret;
  197. }
  198. /* Parse report which is specific to an usage id*/
  199. static int als_parse_report(struct platform_device *pdev,
  200. struct hid_sensor_hub_device *hsdev,
  201. struct iio_chan_spec *channels,
  202. unsigned usage_id,
  203. struct als_state *st)
  204. {
  205. int ret;
  206. ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
  207. usage_id,
  208. HID_USAGE_SENSOR_LIGHT_ILLUM,
  209. &st->als_illum);
  210. if (ret < 0)
  211. return ret;
  212. als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
  213. st->als_illum.size);
  214. dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
  215. st->als_illum.report_id);
  216. return ret;
  217. }
  218. /* Function to initialize the processing for usage id */
  219. static int hid_als_probe(struct platform_device *pdev)
  220. {
  221. int ret = 0;
  222. static const char *name = "als";
  223. struct iio_dev *indio_dev;
  224. struct als_state *als_state;
  225. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  226. struct iio_chan_spec *channels;
  227. indio_dev = iio_device_alloc(sizeof(struct als_state));
  228. if (indio_dev == NULL) {
  229. ret = -ENOMEM;
  230. goto error_ret;
  231. }
  232. platform_set_drvdata(pdev, indio_dev);
  233. als_state = iio_priv(indio_dev);
  234. als_state->common_attributes.hsdev = hsdev;
  235. als_state->common_attributes.pdev = pdev;
  236. ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
  237. &als_state->common_attributes);
  238. if (ret) {
  239. dev_err(&pdev->dev, "failed to setup common attributes\n");
  240. goto error_free_dev;
  241. }
  242. channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL);
  243. if (!channels) {
  244. ret = -ENOMEM;
  245. dev_err(&pdev->dev, "failed to duplicate channels\n");
  246. goto error_free_dev;
  247. }
  248. ret = als_parse_report(pdev, hsdev, channels,
  249. HID_USAGE_SENSOR_ALS, als_state);
  250. if (ret) {
  251. dev_err(&pdev->dev, "failed to setup attributes\n");
  252. goto error_free_dev_mem;
  253. }
  254. indio_dev->channels = channels;
  255. indio_dev->num_channels =
  256. ARRAY_SIZE(als_channels);
  257. indio_dev->dev.parent = &pdev->dev;
  258. indio_dev->info = &als_info;
  259. indio_dev->name = name;
  260. indio_dev->modes = INDIO_DIRECT_MODE;
  261. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  262. NULL, NULL);
  263. if (ret) {
  264. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  265. goto error_free_dev_mem;
  266. }
  267. als_state->common_attributes.data_ready = false;
  268. ret = hid_sensor_setup_trigger(indio_dev, name,
  269. &als_state->common_attributes);
  270. if (ret < 0) {
  271. dev_err(&pdev->dev, "trigger setup failed\n");
  272. goto error_unreg_buffer_funcs;
  273. }
  274. ret = iio_device_register(indio_dev);
  275. if (ret) {
  276. dev_err(&pdev->dev, "device register failed\n");
  277. goto error_remove_trigger;
  278. }
  279. als_state->callbacks.send_event = als_proc_event;
  280. als_state->callbacks.capture_sample = als_capture_sample;
  281. als_state->callbacks.pdev = pdev;
  282. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
  283. &als_state->callbacks);
  284. if (ret < 0) {
  285. dev_err(&pdev->dev, "callback reg failed\n");
  286. goto error_iio_unreg;
  287. }
  288. return ret;
  289. error_iio_unreg:
  290. iio_device_unregister(indio_dev);
  291. error_remove_trigger:
  292. hid_sensor_remove_trigger(indio_dev);
  293. error_unreg_buffer_funcs:
  294. iio_triggered_buffer_cleanup(indio_dev);
  295. error_free_dev_mem:
  296. kfree(indio_dev->channels);
  297. error_free_dev:
  298. iio_device_free(indio_dev);
  299. error_ret:
  300. return ret;
  301. }
  302. /* Function to deinitialize the processing for usage id */
  303. static int hid_als_remove(struct platform_device *pdev)
  304. {
  305. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  306. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  307. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
  308. iio_device_unregister(indio_dev);
  309. hid_sensor_remove_trigger(indio_dev);
  310. iio_triggered_buffer_cleanup(indio_dev);
  311. kfree(indio_dev->channels);
  312. iio_device_free(indio_dev);
  313. return 0;
  314. }
  315. static struct platform_device_id hid_als_ids[] = {
  316. {
  317. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  318. .name = "HID-SENSOR-200041",
  319. },
  320. { /* sentinel */ }
  321. };
  322. MODULE_DEVICE_TABLE(platform, hid_als_ids);
  323. static struct platform_driver hid_als_platform_driver = {
  324. .id_table = hid_als_ids,
  325. .driver = {
  326. .name = KBUILD_MODNAME,
  327. .owner = THIS_MODULE,
  328. },
  329. .probe = hid_als_probe,
  330. .remove = hid_als_remove,
  331. };
  332. module_platform_driver(hid_als_platform_driver);
  333. MODULE_DESCRIPTION("HID Sensor ALS");
  334. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  335. MODULE_LICENSE("GPL");