ucb1400_core.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Core functions for:
  4. * Philips UCB1400 multifunction chip
  5. *
  6. * Based on ucb1400_ts.c:
  7. * Author: Nicolas Pitre
  8. * Created: September 25, 2006
  9. * Copyright: MontaVista Software, Inc.
  10. *
  11. * Spliting done by: Marek Vasut <marek.vasut@gmail.com>
  12. * If something doesn't work and it worked before spliting, e-mail me,
  13. * dont bother Nicolas please ;-)
  14. *
  15. * This code is heavily based on ucb1x00-*.c copyrighted by Russell King
  16. * covering the UCB1100, UCB1200 and UCB1300.. Support for the UCB1400 has
  17. * been made separate from ucb1x00-core/ucb1x00-ts on Russell's request.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/ucb1400.h>
  23. unsigned int ucb1400_adc_read(struct snd_ac97 *ac97, u16 adc_channel,
  24. int adcsync)
  25. {
  26. unsigned int val;
  27. if (adcsync)
  28. adc_channel |= UCB_ADC_SYNC_ENA;
  29. ucb1400_reg_write(ac97, UCB_ADC_CR, UCB_ADC_ENA | adc_channel);
  30. ucb1400_reg_write(ac97, UCB_ADC_CR, UCB_ADC_ENA | adc_channel |
  31. UCB_ADC_START);
  32. while (!((val = ucb1400_reg_read(ac97, UCB_ADC_DATA))
  33. & UCB_ADC_DAT_VALID))
  34. schedule_timeout_uninterruptible(1);
  35. return val & UCB_ADC_DAT_MASK;
  36. }
  37. EXPORT_SYMBOL_GPL(ucb1400_adc_read);
  38. static int ucb1400_core_probe(struct device *dev)
  39. {
  40. int err;
  41. struct ucb1400 *ucb;
  42. struct ucb1400_ts ucb_ts;
  43. struct ucb1400_gpio ucb_gpio;
  44. struct snd_ac97 *ac97;
  45. struct ucb1400_pdata *pdata = dev_get_platdata(dev);
  46. memset(&ucb_ts, 0, sizeof(ucb_ts));
  47. memset(&ucb_gpio, 0, sizeof(ucb_gpio));
  48. ucb = kzalloc(sizeof(struct ucb1400), GFP_KERNEL);
  49. if (!ucb) {
  50. err = -ENOMEM;
  51. goto err;
  52. }
  53. dev_set_drvdata(dev, ucb);
  54. ac97 = to_ac97_t(dev);
  55. ucb_ts.id = ucb1400_reg_read(ac97, UCB_ID);
  56. if (ucb_ts.id != UCB_ID_1400) {
  57. err = -ENODEV;
  58. goto err0;
  59. }
  60. /* GPIO */
  61. ucb_gpio.ac97 = ac97;
  62. if (pdata) {
  63. ucb_gpio.gpio_setup = pdata->gpio_setup;
  64. ucb_gpio.gpio_teardown = pdata->gpio_teardown;
  65. ucb_gpio.gpio_offset = pdata->gpio_offset;
  66. }
  67. ucb->ucb1400_gpio = platform_device_alloc("ucb1400_gpio", -1);
  68. if (!ucb->ucb1400_gpio) {
  69. err = -ENOMEM;
  70. goto err0;
  71. }
  72. err = platform_device_add_data(ucb->ucb1400_gpio, &ucb_gpio,
  73. sizeof(ucb_gpio));
  74. if (err)
  75. goto err1;
  76. err = platform_device_add(ucb->ucb1400_gpio);
  77. if (err)
  78. goto err1;
  79. /* TOUCHSCREEN */
  80. ucb_ts.ac97 = ac97;
  81. if (pdata != NULL && pdata->irq >= 0)
  82. ucb_ts.irq = pdata->irq;
  83. else
  84. ucb_ts.irq = -1;
  85. ucb->ucb1400_ts = platform_device_alloc("ucb1400_ts", -1);
  86. if (!ucb->ucb1400_ts) {
  87. err = -ENOMEM;
  88. goto err2;
  89. }
  90. err = platform_device_add_data(ucb->ucb1400_ts, &ucb_ts,
  91. sizeof(ucb_ts));
  92. if (err)
  93. goto err3;
  94. err = platform_device_add(ucb->ucb1400_ts);
  95. if (err)
  96. goto err3;
  97. return 0;
  98. err3:
  99. platform_device_put(ucb->ucb1400_ts);
  100. err2:
  101. platform_device_del(ucb->ucb1400_gpio);
  102. err1:
  103. platform_device_put(ucb->ucb1400_gpio);
  104. err0:
  105. kfree(ucb);
  106. err:
  107. return err;
  108. }
  109. static int ucb1400_core_remove(struct device *dev)
  110. {
  111. struct ucb1400 *ucb = dev_get_drvdata(dev);
  112. platform_device_unregister(ucb->ucb1400_ts);
  113. platform_device_unregister(ucb->ucb1400_gpio);
  114. kfree(ucb);
  115. return 0;
  116. }
  117. static struct device_driver ucb1400_core_driver = {
  118. .name = "ucb1400_core",
  119. .bus = &ac97_bus_type,
  120. .probe = ucb1400_core_probe,
  121. .remove = ucb1400_core_remove,
  122. };
  123. static int __init ucb1400_core_init(void)
  124. {
  125. return driver_register(&ucb1400_core_driver);
  126. }
  127. static void __exit ucb1400_core_exit(void)
  128. {
  129. driver_unregister(&ucb1400_core_driver);
  130. }
  131. module_init(ucb1400_core_init);
  132. module_exit(ucb1400_core_exit);
  133. MODULE_DESCRIPTION("Philips UCB1400 driver");
  134. MODULE_LICENSE("GPL");