acpi-als.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /* _RAW is here for backward ABI compatibility */
  53. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  54. BIT(IIO_CHAN_INFO_PROCESSED),
  55. },
  56. };
  57. /*
  58. * The event buffer contains timestamp and all the data from
  59. * the ACPI0008 block. There are multiple, but so far we only
  60. * support _ALI (illuminance). Once someone adds new channels
  61. * to acpi_als_channels[], the evt_buffer below will grow
  62. * automatically.
  63. */
  64. #define ACPI_ALS_EVT_NR_SOURCES ARRAY_SIZE(acpi_als_channels)
  65. #define ACPI_ALS_EVT_BUFFER_SIZE \
  66. (sizeof(s64) + (ACPI_ALS_EVT_NR_SOURCES * sizeof(s32)))
  67. struct acpi_als {
  68. struct acpi_device *device;
  69. struct mutex lock;
  70. s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE];
  71. };
  72. /*
  73. * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
  74. * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
  75. * special.
  76. *
  77. * The _ALR property returns tables that can be used to fine-tune the values
  78. * reported by the other props based on the particular hardware type and it's
  79. * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
  80. *
  81. * So far, we support only ALI (illuminance).
  82. */
  83. #define ACPI_ALS_ILLUMINANCE "_ALI"
  84. #define ACPI_ALS_CHROMATICITY "_ALC"
  85. #define ACPI_ALS_COLOR_TEMP "_ALT"
  86. #define ACPI_ALS_POLLING "_ALP"
  87. #define ACPI_ALS_TABLES "_ALR"
  88. static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
  89. {
  90. unsigned long long temp_val;
  91. acpi_status status;
  92. status = acpi_evaluate_integer(als->device->handle, prop, NULL,
  93. &temp_val);
  94. if (ACPI_FAILURE(status)) {
  95. ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop));
  96. return -EIO;
  97. }
  98. *val = temp_val;
  99. return 0;
  100. }
  101. static void acpi_als_notify(struct acpi_device *device, u32 event)
  102. {
  103. struct iio_dev *indio_dev = acpi_driver_data(device);
  104. struct acpi_als *als = iio_priv(indio_dev);
  105. s32 *buffer = als->evt_buffer;
  106. s64 time_ns = iio_get_time_ns(indio_dev);
  107. s32 val;
  108. int ret;
  109. mutex_lock(&als->lock);
  110. memset(buffer, 0, ACPI_ALS_EVT_BUFFER_SIZE);
  111. switch (event) {
  112. case ACPI_ALS_NOTIFY_ILLUMINANCE:
  113. ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
  114. if (ret < 0)
  115. goto out;
  116. *buffer++ = val;
  117. break;
  118. default:
  119. /* Unhandled event */
  120. dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n",
  121. event);
  122. goto out;
  123. }
  124. iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns);
  125. out:
  126. mutex_unlock(&als->lock);
  127. }
  128. static int acpi_als_read_raw(struct iio_dev *indio_dev,
  129. struct iio_chan_spec const *chan, int *val,
  130. int *val2, long mask)
  131. {
  132. struct acpi_als *als = iio_priv(indio_dev);
  133. s32 temp_val;
  134. int ret;
  135. if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
  136. return -EINVAL;
  137. /* we support only illumination (_ALI) so far. */
  138. if (chan->type != IIO_LIGHT)
  139. return -EINVAL;
  140. ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
  141. if (ret < 0)
  142. return ret;
  143. *val = temp_val;
  144. return IIO_VAL_INT;
  145. }
  146. static const struct iio_info acpi_als_info = {
  147. .read_raw = acpi_als_read_raw,
  148. };
  149. static int acpi_als_add(struct acpi_device *device)
  150. {
  151. struct acpi_als *als;
  152. struct iio_dev *indio_dev;
  153. struct iio_buffer *buffer;
  154. indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als));
  155. if (!indio_dev)
  156. return -ENOMEM;
  157. als = iio_priv(indio_dev);
  158. device->driver_data = indio_dev;
  159. als->device = device;
  160. mutex_init(&als->lock);
  161. indio_dev->name = ACPI_ALS_DEVICE_NAME;
  162. indio_dev->dev.parent = &device->dev;
  163. indio_dev->info = &acpi_als_info;
  164. indio_dev->modes = INDIO_BUFFER_SOFTWARE;
  165. indio_dev->channels = acpi_als_channels;
  166. indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
  167. buffer = devm_iio_kfifo_allocate(&device->dev);
  168. if (!buffer)
  169. return -ENOMEM;
  170. iio_device_attach_buffer(indio_dev, buffer);
  171. return devm_iio_device_register(&device->dev, indio_dev);
  172. }
  173. static const struct acpi_device_id acpi_als_device_ids[] = {
  174. {"ACPI0008", 0},
  175. {},
  176. };
  177. MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
  178. static struct acpi_driver acpi_als_driver = {
  179. .name = "acpi_als",
  180. .class = ACPI_ALS_CLASS,
  181. .ids = acpi_als_device_ids,
  182. .ops = {
  183. .add = acpi_als_add,
  184. .notify = acpi_als_notify,
  185. },
  186. };
  187. module_acpi_driver(acpi_als_driver);
  188. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  189. MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
  190. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  191. MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
  192. MODULE_LICENSE("GPL");