hid-sensor-als.c 9.7 KB

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