acpi-als.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. .driver_module = THIS_MODULE,
  148. .read_raw = acpi_als_read_raw,
  149. };
  150. static int acpi_als_add(struct acpi_device *device)
  151. {
  152. struct acpi_als *als;
  153. struct iio_dev *indio_dev;
  154. struct iio_buffer *buffer;
  155. indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als));
  156. if (!indio_dev)
  157. return -ENOMEM;
  158. als = iio_priv(indio_dev);
  159. device->driver_data = indio_dev;
  160. als->device = device;
  161. mutex_init(&als->lock);
  162. indio_dev->name = ACPI_ALS_DEVICE_NAME;
  163. indio_dev->dev.parent = &device->dev;
  164. indio_dev->info = &acpi_als_info;
  165. indio_dev->modes = INDIO_BUFFER_SOFTWARE;
  166. indio_dev->channels = acpi_als_channels;
  167. indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
  168. buffer = devm_iio_kfifo_allocate(&device->dev);
  169. if (!buffer)
  170. return -ENOMEM;
  171. iio_device_attach_buffer(indio_dev, buffer);
  172. return devm_iio_device_register(&device->dev, indio_dev);
  173. }
  174. static const struct acpi_device_id acpi_als_device_ids[] = {
  175. {"ACPI0008", 0},
  176. {},
  177. };
  178. MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
  179. static struct acpi_driver acpi_als_driver = {
  180. .name = "acpi_als",
  181. .class = ACPI_ALS_CLASS,
  182. .ids = acpi_als_device_ids,
  183. .ops = {
  184. .add = acpi_als_add,
  185. .notify = acpi_als_notify,
  186. },
  187. };
  188. module_acpi_driver(acpi_als_driver);
  189. MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
  190. MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
  191. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  192. MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
  193. MODULE_LICENSE("GPL");