acpi-als.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ACPI Ambient Light Sensor Driver
  4. *
  5. * Based on ALS driver:
  6. * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com>
  7. *
  8. * Rework for IIO subsystem:
  9. * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com>
  10. *
  11. * Final cleanup and debugging:
  12. * Copyright (C) 2013-2014 Marek Vasut <marex@denx.de>
  13. * Copyright (C) 2015 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/acpi.h>
  17. #include <linux/err.h>
  18. #include <linux/irq.h>
  19. #include <linux/mutex.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/trigger.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #define ACPI_ALS_CLASS "als"
  26. #define ACPI_ALS_DEVICE_NAME "acpi-als"
  27. #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80
  28. /*
  29. * So far, there's only one channel in here, but the specification for
  30. * ACPI0008 says there can be more to what the block can report. Like
  31. * chromaticity and such. We are ready for incoming additions!
  32. */
  33. static const struct iio_chan_spec acpi_als_channels[] = {
  34. {
  35. .type = IIO_LIGHT,
  36. .scan_type = {
  37. .sign = 's',
  38. .realbits = 32,
  39. .storagebits = 32,
  40. },
  41. /* _RAW is here for backward ABI compatibility */
  42. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  43. BIT(IIO_CHAN_INFO_PROCESSED),
  44. },
  45. IIO_CHAN_SOFT_TIMESTAMP(1),
  46. };
  47. /*
  48. * The event buffer contains timestamp and all the data from
  49. * the ACPI0008 block. There are multiple, but so far we only
  50. * support _ALI (illuminance): One channel, padding and timestamp.
  51. */
  52. #define ACPI_ALS_EVT_BUFFER_SIZE \
  53. (sizeof(s32) + sizeof(s32) + sizeof(s64))
  54. struct acpi_als {
  55. struct acpi_device *device;
  56. struct mutex lock;
  57. struct iio_trigger *trig;
  58. s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)] __aligned(8);
  59. };
  60. /*
  61. * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
  62. * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
  63. * special.
  64. *
  65. * The _ALR property returns tables that can be used to fine-tune the values
  66. * reported by the other props based on the particular hardware type and it's
  67. * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
  68. *
  69. * So far, we support only ALI (illuminance).
  70. */
  71. #define ACPI_ALS_ILLUMINANCE "_ALI"
  72. #define ACPI_ALS_CHROMATICITY "_ALC"
  73. #define ACPI_ALS_COLOR_TEMP "_ALT"
  74. #define ACPI_ALS_POLLING "_ALP"
  75. #define ACPI_ALS_TABLES "_ALR"
  76. static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
  77. {
  78. unsigned long long temp_val;
  79. acpi_status status;
  80. status = acpi_evaluate_integer(als->device->handle, prop, NULL,
  81. &temp_val);
  82. if (ACPI_FAILURE(status)) {
  83. acpi_evaluation_failure_warn(als->device->handle, prop, status);
  84. return -EIO;
  85. }
  86. *val = temp_val;
  87. return 0;
  88. }
  89. static void acpi_als_notify(struct acpi_device *device, u32 event)
  90. {
  91. struct iio_dev *indio_dev = acpi_driver_data(device);
  92. struct acpi_als *als = iio_priv(indio_dev);
  93. if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) {
  94. switch (event) {
  95. case ACPI_ALS_NOTIFY_ILLUMINANCE:
  96. iio_trigger_poll_nested(als->trig);
  97. break;
  98. default:
  99. /* Unhandled event */
  100. dev_dbg(&device->dev,
  101. "Unhandled ACPI ALS event (%08x)!\n",
  102. event);
  103. }
  104. }
  105. }
  106. static int acpi_als_read_raw(struct iio_dev *indio_dev,
  107. struct iio_chan_spec const *chan, int *val,
  108. int *val2, long mask)
  109. {
  110. struct acpi_als *als = iio_priv(indio_dev);
  111. s32 temp_val;
  112. int ret;
  113. if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
  114. return -EINVAL;
  115. /* we support only illumination (_ALI) so far. */
  116. if (chan->type != IIO_LIGHT)
  117. return -EINVAL;
  118. ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
  119. if (ret < 0)
  120. return ret;
  121. *val = temp_val;
  122. return IIO_VAL_INT;
  123. }
  124. static const struct iio_info acpi_als_info = {
  125. .read_raw = acpi_als_read_raw,
  126. };
  127. static irqreturn_t acpi_als_trigger_handler(int irq, void *p)
  128. {
  129. struct iio_poll_func *pf = p;
  130. struct iio_dev *indio_dev = pf->indio_dev;
  131. struct acpi_als *als = iio_priv(indio_dev);
  132. s32 *buffer = als->evt_buffer;
  133. s32 val;
  134. int ret;
  135. mutex_lock(&als->lock);
  136. ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
  137. if (ret < 0)
  138. goto out;
  139. *buffer = val;
  140. /*
  141. * When coming from own trigger via polls, set polling function
  142. * timestamp here. Given ACPI notifier is already in a thread and call
  143. * function directly, there is no need to set the timestamp in the
  144. * notify function.
  145. *
  146. * If the timestamp was actually 0, the timestamp is set one more time.
  147. */
  148. if (!pf->timestamp)
  149. pf->timestamp = iio_get_time_ns(indio_dev);
  150. iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
  151. out:
  152. mutex_unlock(&als->lock);
  153. iio_trigger_notify_done(indio_dev->trig);
  154. return IRQ_HANDLED;
  155. }
  156. static int acpi_als_add(struct acpi_device *device)
  157. {
  158. struct device *dev = &device->dev;
  159. struct iio_dev *indio_dev;
  160. struct acpi_als *als;
  161. int ret;
  162. indio_dev = devm_iio_device_alloc(dev, sizeof(*als));
  163. if (!indio_dev)
  164. return -ENOMEM;
  165. als = iio_priv(indio_dev);
  166. device->driver_data = indio_dev;
  167. als->device = device;
  168. mutex_init(&als->lock);
  169. indio_dev->name = ACPI_ALS_DEVICE_NAME;
  170. indio_dev->info = &acpi_als_info;
  171. indio_dev->channels = acpi_als_channels;
  172. indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
  173. als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
  174. iio_device_id(indio_dev));
  175. if (!als->trig)
  176. return -ENOMEM;
  177. ret = devm_iio_trigger_register(dev, als->trig);
  178. if (ret)
  179. return ret;
  180. /*
  181. * Set hardware trigger by default to let events flow when
  182. * BIOS support notification.
  183. */
  184. indio_dev->trig = iio_trigger_get(als->trig);
  185. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  186. iio_pollfunc_store_time,
  187. acpi_als_trigger_handler,
  188. NULL);
  189. if (ret)
  190. return ret;
  191. return devm_iio_device_register(dev, indio_dev);
  192. }
  193. static const struct acpi_device_id acpi_als_device_ids[] = {
  194. {"ACPI0008", 0},
  195. {},
  196. };
  197. MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
  198. static struct acpi_driver acpi_als_driver = {
  199. .name = "acpi_als",
  200. .class = ACPI_ALS_CLASS,
  201. .ids = acpi_als_device_ids,
  202. .ops = {
  203. .add = acpi_als_add,
  204. .notify = acpi_als_notify,
  205. },
  206. };
  207. module_acpi_driver(acpi_als_driver);
  208. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  209. MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
  210. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  211. MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
  212. MODULE_LICENSE("GPL");