hid-sensor-als.c 10 KB

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