acpi-als.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * ACPI Ambient Light Sensor Driver
  3. *
  4. * Based on ALS driver:
  5. * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com>
  6. *
  7. * Rework for IIO subsystem:
  8. * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com>
  9. *
  10. * Final cleanup and debugging:
  11. * Copyright (C) 2013-2014 Marek Vasut <marex@denx.de>
  12. * Copyright (C) 2015 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/acpi.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. #include <linux/iio/iio.h>
  33. #include <linux/iio/buffer.h>
  34. #include <linux/iio/kfifo_buf.h>
  35. #define ACPI_ALS_CLASS "als"
  36. #define ACPI_ALS_DEVICE_NAME "acpi-als"
  37. #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80
  38. ACPI_MODULE_NAME("acpi-als");
  39. /*
  40. * So far, there's only one channel in here, but the specification for
  41. * ACPI0008 says there can be more to what the block can report. Like
  42. * chromaticity and such. We are ready for incoming additions!
  43. */
  44. static const struct iio_chan_spec acpi_als_channels[] = {
  45. {
  46. .type = IIO_LIGHT,
  47. .scan_type = {
  48. .sign = 's',
  49. .realbits = 32,
  50. .storagebits = 32,
  51. },
  52. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  53. },
  54. };
  55. /*
  56. * The event buffer contains timestamp and all the data from
  57. * the ACPI0008 block. There are multiple, but so far we only
  58. * support _ALI (illuminance). Once someone adds new channels
  59. * to acpi_als_channels[], the evt_buffer below will grow
  60. * automatically.
  61. */
  62. #define ACPI_ALS_EVT_NR_SOURCES ARRAY_SIZE(acpi_als_channels)
  63. #define ACPI_ALS_EVT_BUFFER_SIZE \
  64. (sizeof(s64) + (ACPI_ALS_EVT_NR_SOURCES * sizeof(s32)))
  65. struct acpi_als {
  66. struct acpi_device *device;
  67. struct mutex lock;
  68. s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE];
  69. };
  70. /*
  71. * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
  72. * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
  73. * special.
  74. *
  75. * The _ALR property returns tables that can be used to fine-tune the values
  76. * reported by the other props based on the particular hardware type and it's
  77. * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
  78. *
  79. * So far, we support only ALI (illuminance).
  80. */
  81. #define ACPI_ALS_ILLUMINANCE "_ALI"
  82. #define ACPI_ALS_CHROMATICITY "_ALC"
  83. #define ACPI_ALS_COLOR_TEMP "_ALT"
  84. #define ACPI_ALS_POLLING "_ALP"
  85. #define ACPI_ALS_TABLES "_ALR"
  86. static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
  87. {
  88. unsigned long long temp_val;
  89. acpi_status status;
  90. status = acpi_evaluate_integer(als->device->handle, prop, NULL,
  91. &temp_val);
  92. if (ACPI_FAILURE(status)) {
  93. ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop));
  94. return -EIO;
  95. }
  96. *val = temp_val;
  97. return 0;
  98. }
  99. static void acpi_als_notify(struct acpi_device *device, u32 event)
  100. {
  101. struct iio_dev *indio_dev = acpi_driver_data(device);
  102. struct acpi_als *als = iio_priv(indio_dev);
  103. s32 *buffer = als->evt_buffer;
  104. s64 time_ns = iio_get_time_ns();
  105. s32 val;
  106. int ret;
  107. mutex_lock(&als->lock);
  108. memset(buffer, 0, ACPI_ALS_EVT_BUFFER_SIZE);
  109. switch (event) {
  110. case ACPI_ALS_NOTIFY_ILLUMINANCE:
  111. ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
  112. if (ret < 0)
  113. goto out;
  114. *buffer++ = val;
  115. break;
  116. default:
  117. /* Unhandled event */
  118. dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n",
  119. event);
  120. goto out;
  121. }
  122. iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns);
  123. out:
  124. mutex_unlock(&als->lock);
  125. }
  126. static int acpi_als_read_raw(struct iio_dev *indio_dev,
  127. struct iio_chan_spec const *chan, int *val,
  128. int *val2, long mask)
  129. {
  130. struct acpi_als *als = iio_priv(indio_dev);
  131. s32 temp_val;
  132. int ret;
  133. if (mask != IIO_CHAN_INFO_RAW)
  134. return -EINVAL;
  135. /* we support only illumination (_ALI) so far. */
  136. if (chan->type != IIO_LIGHT)
  137. return -EINVAL;
  138. ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
  139. if (ret < 0)
  140. return ret;
  141. *val = temp_val;
  142. return IIO_VAL_INT;
  143. }
  144. static const struct iio_info acpi_als_info = {
  145. .driver_module = THIS_MODULE,
  146. .read_raw = acpi_als_read_raw,
  147. };
  148. static int acpi_als_add(struct acpi_device *device)
  149. {
  150. struct acpi_als *als;
  151. struct iio_dev *indio_dev;
  152. struct iio_buffer *buffer;
  153. indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als));
  154. if (!indio_dev)
  155. return -ENOMEM;
  156. als = iio_priv(indio_dev);
  157. device->driver_data = indio_dev;
  158. als->device = device;
  159. mutex_init(&als->lock);
  160. indio_dev->name = ACPI_ALS_DEVICE_NAME;
  161. indio_dev->dev.parent = &device->dev;
  162. indio_dev->info = &acpi_als_info;
  163. indio_dev->modes = INDIO_BUFFER_SOFTWARE;
  164. indio_dev->channels = acpi_als_channels;
  165. indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
  166. buffer = devm_iio_kfifo_allocate(&device->dev);
  167. if (!buffer)
  168. return -ENOMEM;
  169. iio_device_attach_buffer(indio_dev, buffer);
  170. return devm_iio_device_register(&device->dev, indio_dev);
  171. }
  172. static const struct acpi_device_id acpi_als_device_ids[] = {
  173. {"ACPI0008", 0},
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
  177. static struct acpi_driver acpi_als_driver = {
  178. .name = "acpi_als",
  179. .class = ACPI_ALS_CLASS,
  180. .ids = acpi_als_device_ids,
  181. .ops = {
  182. .add = acpi_als_add,
  183. .notify = acpi_als_notify,
  184. },
  185. };
  186. module_acpi_driver(acpi_als_driver);
  187. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  188. MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
  189. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  190. MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
  191. MODULE_LICENSE("GPL");