intel_mid_powerbtn.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Power button driver for Intel MID platforms.
  3. *
  4. * Copyright (C) 2010,2017 Intel Corp
  5. *
  6. * Author: Hong Liu <hong.liu@intel.com>
  7. * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/input.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mfd/intel_msic.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_wakeirq.h>
  25. #include <linux/slab.h>
  26. #include <asm/cpu_device_id.h>
  27. #include <asm/intel-family.h>
  28. #include <asm/intel_scu_ipc.h>
  29. #define DRIVER_NAME "msic_power_btn"
  30. #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
  31. /*
  32. * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
  33. * power button interrupt
  34. */
  35. #define MSIC_PWRBTNM (1 << 0)
  36. /* Intel Tangier */
  37. #define BCOVE_PB_LEVEL (1 << 4) /* 1 - release, 0 - press */
  38. /* Basin Cove PMIC */
  39. #define BCOVE_PBIRQ 0x02
  40. #define BCOVE_IRQLVL1MSK 0x0c
  41. #define BCOVE_PBIRQMASK 0x0d
  42. #define BCOVE_PBSTATUS 0x27
  43. struct mid_pb_ddata {
  44. struct device *dev;
  45. int irq;
  46. struct input_dev *input;
  47. unsigned short mirqlvl1_addr;
  48. unsigned short pbstat_addr;
  49. u8 pbstat_mask;
  50. int (*setup)(struct mid_pb_ddata *ddata);
  51. };
  52. static int mid_pbstat(struct mid_pb_ddata *ddata, int *value)
  53. {
  54. struct input_dev *input = ddata->input;
  55. int ret;
  56. u8 pbstat;
  57. ret = intel_scu_ipc_ioread8(ddata->pbstat_addr, &pbstat);
  58. if (ret)
  59. return ret;
  60. dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
  61. *value = !(pbstat & ddata->pbstat_mask);
  62. return 0;
  63. }
  64. static int mid_irq_ack(struct mid_pb_ddata *ddata)
  65. {
  66. return intel_scu_ipc_update_register(ddata->mirqlvl1_addr, 0, MSIC_PWRBTNM);
  67. }
  68. static int mrfld_setup(struct mid_pb_ddata *ddata)
  69. {
  70. /* Unmask the PBIRQ and MPBIRQ on Tangier */
  71. intel_scu_ipc_update_register(BCOVE_PBIRQ, 0, MSIC_PWRBTNM);
  72. intel_scu_ipc_update_register(BCOVE_PBIRQMASK, 0, MSIC_PWRBTNM);
  73. return 0;
  74. }
  75. static irqreturn_t mid_pb_isr(int irq, void *dev_id)
  76. {
  77. struct mid_pb_ddata *ddata = dev_id;
  78. struct input_dev *input = ddata->input;
  79. int value = 0;
  80. int ret;
  81. ret = mid_pbstat(ddata, &value);
  82. if (ret < 0) {
  83. dev_err(input->dev.parent,
  84. "Read error %d while reading MSIC_PB_STATUS\n", ret);
  85. } else {
  86. input_event(input, EV_KEY, KEY_POWER, value);
  87. input_sync(input);
  88. }
  89. mid_irq_ack(ddata);
  90. return IRQ_HANDLED;
  91. }
  92. static const struct mid_pb_ddata mfld_ddata = {
  93. .mirqlvl1_addr = INTEL_MSIC_IRQLVL1MSK,
  94. .pbstat_addr = INTEL_MSIC_PBSTATUS,
  95. .pbstat_mask = MSIC_PB_LEVEL,
  96. };
  97. static const struct mid_pb_ddata mrfld_ddata = {
  98. .mirqlvl1_addr = BCOVE_IRQLVL1MSK,
  99. .pbstat_addr = BCOVE_PBSTATUS,
  100. .pbstat_mask = BCOVE_PB_LEVEL,
  101. .setup = mrfld_setup,
  102. };
  103. #define ICPU(model, ddata) \
  104. { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (kernel_ulong_t)&ddata }
  105. static const struct x86_cpu_id mid_pb_cpu_ids[] = {
  106. ICPU(INTEL_FAM6_ATOM_SALTWELL_MID, mfld_ddata),
  107. ICPU(INTEL_FAM6_ATOM_SILVERMONT_MID, mrfld_ddata),
  108. {}
  109. };
  110. static int mid_pb_probe(struct platform_device *pdev)
  111. {
  112. const struct x86_cpu_id *id;
  113. struct mid_pb_ddata *ddata;
  114. struct input_dev *input;
  115. int irq = platform_get_irq(pdev, 0);
  116. int error;
  117. id = x86_match_cpu(mid_pb_cpu_ids);
  118. if (!id)
  119. return -ENODEV;
  120. if (irq < 0) {
  121. dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  122. return irq;
  123. }
  124. input = devm_input_allocate_device(&pdev->dev);
  125. if (!input)
  126. return -ENOMEM;
  127. input->name = pdev->name;
  128. input->phys = "power-button/input0";
  129. input->id.bustype = BUS_HOST;
  130. input->dev.parent = &pdev->dev;
  131. input_set_capability(input, EV_KEY, KEY_POWER);
  132. ddata = devm_kmemdup(&pdev->dev, (void *)id->driver_data,
  133. sizeof(*ddata), GFP_KERNEL);
  134. if (!ddata)
  135. return -ENOMEM;
  136. ddata->dev = &pdev->dev;
  137. ddata->irq = irq;
  138. ddata->input = input;
  139. if (ddata->setup) {
  140. error = ddata->setup(ddata);
  141. if (error)
  142. return error;
  143. }
  144. error = devm_request_threaded_irq(&pdev->dev, irq, NULL, mid_pb_isr,
  145. IRQF_ONESHOT, DRIVER_NAME, ddata);
  146. if (error) {
  147. dev_err(&pdev->dev,
  148. "Unable to request irq %d for MID power button\n", irq);
  149. return error;
  150. }
  151. error = input_register_device(input);
  152. if (error) {
  153. dev_err(&pdev->dev,
  154. "Unable to register input dev, error %d\n", error);
  155. return error;
  156. }
  157. platform_set_drvdata(pdev, ddata);
  158. /*
  159. * SCU firmware might send power button interrupts to IA core before
  160. * kernel boots and doesn't get EOI from IA core. The first bit of
  161. * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
  162. * power interrupt to Android kernel. Unmask the bit when probing
  163. * power button in kernel.
  164. * There is a very narrow race between irq handler and power button
  165. * initialization. The race happens rarely. So we needn't worry
  166. * about it.
  167. */
  168. error = mid_irq_ack(ddata);
  169. if (error) {
  170. dev_err(&pdev->dev,
  171. "Unable to clear power button interrupt, error: %d\n",
  172. error);
  173. return error;
  174. }
  175. device_init_wakeup(&pdev->dev, true);
  176. dev_pm_set_wake_irq(&pdev->dev, irq);
  177. return 0;
  178. }
  179. static int mid_pb_remove(struct platform_device *pdev)
  180. {
  181. dev_pm_clear_wake_irq(&pdev->dev);
  182. device_init_wakeup(&pdev->dev, false);
  183. return 0;
  184. }
  185. static struct platform_driver mid_pb_driver = {
  186. .driver = {
  187. .name = DRIVER_NAME,
  188. },
  189. .probe = mid_pb_probe,
  190. .remove = mid_pb_remove,
  191. };
  192. module_platform_driver(mid_pb_driver);
  193. MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
  194. MODULE_DESCRIPTION("Intel MID Power Button Driver");
  195. MODULE_LICENSE("GPL v2");
  196. MODULE_ALIAS("platform:" DRIVER_NAME);