arizona-spi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arizona-spi.c -- Arizona SPI bus interface
  4. *
  5. * Copyright 2012 Wolfson Microelectronics plc
  6. *
  7. * Author: Mark Brown <[email protected]>
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/gpio/machine.h>
  13. #include <linux/module.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regmap.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/of.h>
  20. #include <uapi/linux/input-event-codes.h>
  21. #include <linux/mfd/arizona/core.h>
  22. #include "arizona.h"
  23. #ifdef CONFIG_ACPI
  24. static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
  25. static const struct acpi_gpio_params ldoena_gpios = { 2, 0, false };
  26. static const struct acpi_gpio_mapping arizona_acpi_gpios[] = {
  27. { "reset-gpios", &reset_gpios, 1, },
  28. { "wlf,ldoena-gpios", &ldoena_gpios, 1 },
  29. { }
  30. };
  31. /*
  32. * The ACPI resources for the device only describe external GPIO-s. They do
  33. * not provide mappings for the GPIO-s coming from the Arizona codec itself.
  34. */
  35. static const struct gpiod_lookup arizona_soc_gpios[] = {
  36. { "arizona", 2, "wlf,spkvdd-ena", 0, GPIO_ACTIVE_HIGH },
  37. { "arizona", 4, "wlf,micd-pol", 0, GPIO_ACTIVE_LOW },
  38. };
  39. static void arizona_spi_acpi_remove_lookup(void *lookup)
  40. {
  41. gpiod_remove_lookup_table(lookup);
  42. }
  43. /* For ACPI tables from boards which ship with Windows as factory OS */
  44. static int arizona_spi_acpi_windows_probe(struct arizona *arizona)
  45. {
  46. struct gpiod_lookup_table *lookup;
  47. acpi_status status;
  48. int ret;
  49. /* Add mappings for the 2 ACPI declared GPIOs used for reset and ldo-ena */
  50. devm_acpi_dev_add_driver_gpios(arizona->dev, arizona_acpi_gpios);
  51. /* Add lookups for the SoCs own GPIOs used for micdet-polarity and spkVDD-enable */
  52. lookup = devm_kzalloc(arizona->dev,
  53. struct_size(lookup, table, ARRAY_SIZE(arizona_soc_gpios) + 1),
  54. GFP_KERNEL);
  55. if (!lookup)
  56. return -ENOMEM;
  57. lookup->dev_id = dev_name(arizona->dev);
  58. memcpy(lookup->table, arizona_soc_gpios, sizeof(arizona_soc_gpios));
  59. gpiod_add_lookup_table(lookup);
  60. ret = devm_add_action_or_reset(arizona->dev, arizona_spi_acpi_remove_lookup, lookup);
  61. if (ret)
  62. return ret;
  63. /* Enable 32KHz clock from SoC to codec for jack-detect */
  64. status = acpi_evaluate_object(ACPI_HANDLE(arizona->dev), "CLKE", NULL, NULL);
  65. if (ACPI_FAILURE(status))
  66. dev_warn(arizona->dev, "Failed to enable 32KHz clk ACPI error %d\n", status);
  67. return 0;
  68. }
  69. /* For ACPI tables from boards which ship with Android as factory OS */
  70. static int arizona_spi_acpi_android_probe(struct arizona *arizona)
  71. {
  72. int ret;
  73. /*
  74. * Get the reset GPIO, treating -ENOENT as -EPROBE_DEFER to wait for
  75. * the x86-android-tablets module to register the board specific GPIO
  76. * lookup table.
  77. */
  78. arizona->pdata.reset = devm_gpiod_get(arizona->dev, "reset", GPIOD_OUT_LOW);
  79. if (IS_ERR(arizona->pdata.reset)) {
  80. ret = PTR_ERR(arizona->pdata.reset);
  81. if (ret == -ENOENT) {
  82. dev_info_once(arizona->dev,
  83. "Deferring probe till GPIO lookup is registered\n");
  84. ret = -EPROBE_DEFER;
  85. }
  86. return dev_err_probe(arizona->dev, ret, "getting reset GPIO\n");
  87. }
  88. return 0;
  89. }
  90. /*
  91. * The AOSP 3.5 mm Headset: Accessory Specification gives the following values:
  92. * Function A Play/Pause: 0 ohm
  93. * Function D Voice assistant: 135 ohm
  94. * Function B Volume Up 240 ohm
  95. * Function C Volume Down 470 ohm
  96. * Minimum Mic DC resistance 1000 ohm
  97. * Minimum Ear speaker impedance 16 ohm
  98. * Note the first max value below must be less then the min. speaker impedance,
  99. * to allow CTIA/OMTP detection to work. The other max values are the closest
  100. * value from extcon-arizona.c:arizona_micd_levels halfway 2 button resistances.
  101. */
  102. static const struct arizona_micd_range arizona_micd_aosp_ranges[] = {
  103. { .max = 11, .key = KEY_PLAYPAUSE },
  104. { .max = 186, .key = KEY_VOICECOMMAND },
  105. { .max = 348, .key = KEY_VOLUMEUP },
  106. { .max = 752, .key = KEY_VOLUMEDOWN },
  107. };
  108. static int arizona_spi_acpi_probe(struct arizona *arizona)
  109. {
  110. struct acpi_device *adev = ACPI_COMPANION(arizona->dev);
  111. int ret;
  112. if (acpi_dev_hid_uid_match(adev, "10WM5102", NULL))
  113. ret = arizona_spi_acpi_android_probe(arizona);
  114. else
  115. ret = arizona_spi_acpi_windows_probe(arizona);
  116. if (ret)
  117. return ret;
  118. /*
  119. * Some DSDTs wrongly declare the IRQ trigger-type as IRQF_TRIGGER_FALLING
  120. * The IRQ line will stay low when a new IRQ event happens between reading
  121. * the IRQ status flags and acknowledging them. When the IRQ line stays
  122. * low like this the IRQ will never trigger again when its type is set
  123. * to IRQF_TRIGGER_FALLING. Correct the IRQ trigger-type to fix this.
  124. *
  125. * Note theoretically it is possible that some boards are not capable
  126. * of handling active low level interrupts. In that case setting the
  127. * flag to IRQF_TRIGGER_FALLING would not be a bug (and we would need
  128. * to work around this) but so far all known usages of IRQF_TRIGGER_FALLING
  129. * are a bug in the board's DSDT.
  130. */
  131. arizona->pdata.irq_flags = IRQF_TRIGGER_LOW;
  132. /* Wait 200 ms after jack insertion */
  133. arizona->pdata.micd_detect_debounce = 200;
  134. /* Use standard AOSP values for headset-button mappings */
  135. arizona->pdata.micd_ranges = arizona_micd_aosp_ranges;
  136. arizona->pdata.num_micd_ranges = ARRAY_SIZE(arizona_micd_aosp_ranges);
  137. /* Use left headphone speaker for HP vs line-out detection */
  138. arizona->pdata.hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
  139. return 0;
  140. }
  141. static const struct acpi_device_id arizona_acpi_match[] = {
  142. {
  143. .id = "WM510204",
  144. .driver_data = WM5102,
  145. },
  146. {
  147. .id = "WM510205",
  148. .driver_data = WM5102,
  149. },
  150. {
  151. .id = "10WM5102",
  152. .driver_data = WM5102,
  153. },
  154. { }
  155. };
  156. MODULE_DEVICE_TABLE(acpi, arizona_acpi_match);
  157. #else
  158. static int arizona_spi_acpi_probe(struct arizona *arizona)
  159. {
  160. return -ENODEV;
  161. }
  162. #endif
  163. static int arizona_spi_probe(struct spi_device *spi)
  164. {
  165. const struct spi_device_id *id = spi_get_device_id(spi);
  166. const void *match_data;
  167. struct arizona *arizona;
  168. const struct regmap_config *regmap_config = NULL;
  169. unsigned long type = 0;
  170. int ret;
  171. match_data = device_get_match_data(&spi->dev);
  172. if (match_data)
  173. type = (unsigned long)match_data;
  174. else if (id)
  175. type = id->driver_data;
  176. switch (type) {
  177. case WM5102:
  178. if (IS_ENABLED(CONFIG_MFD_WM5102))
  179. regmap_config = &wm5102_spi_regmap;
  180. break;
  181. case WM5110:
  182. case WM8280:
  183. if (IS_ENABLED(CONFIG_MFD_WM5110))
  184. regmap_config = &wm5110_spi_regmap;
  185. break;
  186. case WM1831:
  187. case CS47L24:
  188. if (IS_ENABLED(CONFIG_MFD_CS47L24))
  189. regmap_config = &cs47l24_spi_regmap;
  190. break;
  191. default:
  192. dev_err(&spi->dev, "Unknown device type %ld\n", type);
  193. return -EINVAL;
  194. }
  195. if (!regmap_config) {
  196. dev_err(&spi->dev,
  197. "No kernel support for device type %ld\n", type);
  198. return -EINVAL;
  199. }
  200. arizona = devm_kzalloc(&spi->dev, sizeof(*arizona), GFP_KERNEL);
  201. if (arizona == NULL)
  202. return -ENOMEM;
  203. arizona->regmap = devm_regmap_init_spi(spi, regmap_config);
  204. if (IS_ERR(arizona->regmap)) {
  205. ret = PTR_ERR(arizona->regmap);
  206. dev_err(&spi->dev, "Failed to allocate register map: %d\n",
  207. ret);
  208. return ret;
  209. }
  210. arizona->type = type;
  211. arizona->dev = &spi->dev;
  212. arizona->irq = spi->irq;
  213. if (has_acpi_companion(&spi->dev)) {
  214. ret = arizona_spi_acpi_probe(arizona);
  215. if (ret)
  216. return ret;
  217. }
  218. return arizona_dev_init(arizona);
  219. }
  220. static void arizona_spi_remove(struct spi_device *spi)
  221. {
  222. struct arizona *arizona = spi_get_drvdata(spi);
  223. arizona_dev_exit(arizona);
  224. }
  225. static const struct spi_device_id arizona_spi_ids[] = {
  226. { "wm5102", WM5102 },
  227. { "wm5110", WM5110 },
  228. { "wm8280", WM8280 },
  229. { "wm1831", WM1831 },
  230. { "cs47l24", CS47L24 },
  231. { },
  232. };
  233. MODULE_DEVICE_TABLE(spi, arizona_spi_ids);
  234. #ifdef CONFIG_OF
  235. static const struct of_device_id arizona_spi_of_match[] = {
  236. { .compatible = "wlf,wm5102", .data = (void *)WM5102 },
  237. { .compatible = "wlf,wm5110", .data = (void *)WM5110 },
  238. { .compatible = "wlf,wm8280", .data = (void *)WM8280 },
  239. { .compatible = "wlf,wm1831", .data = (void *)WM1831 },
  240. { .compatible = "cirrus,cs47l24", .data = (void *)CS47L24 },
  241. {},
  242. };
  243. MODULE_DEVICE_TABLE(of, arizona_spi_of_match);
  244. #endif
  245. static struct spi_driver arizona_spi_driver = {
  246. .driver = {
  247. .name = "arizona",
  248. .pm = &arizona_pm_ops,
  249. .of_match_table = of_match_ptr(arizona_spi_of_match),
  250. .acpi_match_table = ACPI_PTR(arizona_acpi_match),
  251. },
  252. .probe = arizona_spi_probe,
  253. .remove = arizona_spi_remove,
  254. .id_table = arizona_spi_ids,
  255. };
  256. module_spi_driver(arizona_spi_driver);
  257. MODULE_SOFTDEP("pre: arizona_ldo1");
  258. MODULE_DESCRIPTION("Arizona SPI bus interface");
  259. MODULE_AUTHOR("Mark Brown <[email protected]>");
  260. MODULE_LICENSE("GPL");