hid-sensor-als.c 9.8 KB

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