Debian Codegraph @

drivers/pwm/pwm-dwc.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * DesignWare PWM Controller driver (PCI part)
4 *
5 * Copyright (C) 2018-2020 Intel Corporation
6 *
7 * Author: Felipe Balbi (Intel)
8 * Author: Jarkko Nikula <[email protected]>
9 * Author: Raymond Tan <[email protected]>
10 *
11 * Limitations:
12 * - The hardware cannot generate a 0 % or 100 % duty cycle. Both high and low
13 * periods are one or more input clock periods long.
14 */
15
16 #define DEFAULT_MOUDLE_NAMESPACE dwc_pwm
17
18 #include <linux/bitops.h>
19 #include <linux/export.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/pwm.h>
25 #include <linux/sizes.h>
26
27 #include "pwm-dwc.h"
28
29 /* Elkhart Lake */
30 static const struct dwc_pwm_info ehl_pwm_info = {
31 .nr = 2,
32 .size = SZ_4K,
33 };
34
35 static int dwc_pwm_init_one(struct device *dev, struct dwc_pwm_drvdata *ddata, unsigned int idx)
36 {
37 struct pwm_chip *chip;
38 struct dwc_pwm *dwc;
39 int ret;
40
41 chip = dwc_pwm_alloc(dev);
42 if (IS_ERR(chip))
43 return PTR_ERR(chip);
44
45 dwc = to_dwc_pwm(chip);
46 dwc->base = ddata->io_base + (ddata->info->size * idx);
47
48 ret = devm_pwmchip_add(dev, chip);
49 if (ret)
50 return ret;
51
52 ddata->chips[idx] = chip;
53 return 0;
54 }
55
56 static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
57 {
58 const struct dwc_pwm_info *info;
59 struct device *dev = &pci->dev;
60 struct dwc_pwm_drvdata *ddata;
61 unsigned int idx;
62 int ret;
63
64 ret = pcim_enable_device(pci);
65 if (ret)
66 return dev_err_probe(dev, ret, "Failed to enable device\n");
67
68 pci_set_master(pci);
69
70 info = (const struct dwc_pwm_info *)id->driver_data;
71 ddata = devm_kzalloc(dev, struct_size(ddata, chips, info->nr), GFP_KERNEL);
72 if (!ddata)
73 return -ENOMEM;
74
75 ddata->io_base = pcim_iomap_region(pci, 0, "pwm-dwc");
76 if (IS_ERR(ddata->io_base))
77 return dev_err_probe(dev, PTR_ERR(ddata->io_base),
78 "Failed to request / iomap PCI BAR\n");
79
80 ddata->info = info;
81
82 for (idx = 0; idx < ddata->info->nr; idx++) {
83 ret = dwc_pwm_init_one(dev, ddata, idx);
84 if (ret)
85 return ret;
86 }
87
88 dev_set_drvdata(dev, ddata);
89
90 pm_runtime_put(dev);
91 pm_runtime_allow(dev);
92
93 return 0;
94 }
95
96 static void dwc_pwm_remove(struct pci_dev *pci)
97 {
98 pm_runtime_forbid(&pci->dev);
99 pm_runtime_get_noresume(&pci->dev);
100 }
101
102 static int dwc_pwm_suspend(struct device *dev)
103 {
104 struct dwc_pwm_drvdata *ddata = dev_get_drvdata(dev);
105 unsigned int idx;
106
107 for (idx = 0; idx < ddata->info->nr; idx++) {
108 struct pwm_chip *chip = ddata->chips[idx];
109 struct dwc_pwm *dwc = to_dwc_pwm(chip);
110 unsigned int i;
111
112 for (i = 0; i < DWC_TIMERS_TOTAL; i++) {
113 if (chip->pwms[i].state.enabled) {
114 dev_err(dev, "PWM %u in use by consumer (%s)\n",
115 i, chip->pwms[i].label);
116 return -EBUSY;
117 }
118 dwc->ctx[i].cnt = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(i));
119 dwc->ctx[i].cnt2 = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(i));
120 dwc->ctx[i].ctrl = dwc_pwm_readl(dwc, DWC_TIM_CTRL(i));
121 }
122 }
123
124 return 0;
125 }
126
127 static int dwc_pwm_resume(struct device *dev)
128 {
129 struct dwc_pwm_drvdata *ddata = dev_get_drvdata(dev);
130 unsigned int idx;
131
132 for (idx = 0; idx < ddata->info->nr; idx++) {
133 struct pwm_chip *chip = ddata->chips[idx];
134 struct dwc_pwm *dwc = to_dwc_pwm(chip);
135 unsigned int i;
136
137 for (i = 0; i < DWC_TIMERS_TOTAL; i++) {
138 dwc_pwm_writel(dwc, dwc->ctx[i].cnt, DWC_TIM_LD_CNT(i));
139 dwc_pwm_writel(dwc, dwc->ctx[i].cnt2, DWC_TIM_LD_CNT2(i));
140 dwc_pwm_writel(dwc, dwc->ctx[i].ctrl, DWC_TIM_CTRL(i));
141 }
142 }
143
144 return 0;
145 }
146
147 static DEFINE_SIMPLE_DEV_PM_OPS(dwc_pwm_pm_ops, dwc_pwm_suspend, dwc_pwm_resume);
148
149 static const struct pci_device_id dwc_pwm_id_table[] = {
150 { PCI_VDEVICE(INTEL, 0x4bb7), (kernel_ulong_t)&ehl_pwm_info },
151 { } /* Terminating Entry */
152 };
153 MODULE_DEVICE_TABLE(pci, dwc_pwm_id_table);
154
155 static struct pci_driver dwc_pwm_driver = {
156 .name = "pwm-dwc",
157 .probe = dwc_pwm_probe,
158 .remove = dwc_pwm_remove,
159 .id_table = dwc_pwm_id_table,
160 .driver = {
161 .pm = pm_sleep_ptr(&dwc_pwm_pm_ops),
162 },
163 };
164
165 module_pci_driver(dwc_pwm_driver);
166
167 MODULE_AUTHOR("Felipe Balbi (Intel)");
168 MODULE_AUTHOR("Jarkko Nikula <[email protected]>");
169 MODULE_AUTHOR("Raymond Tan <[email protected]>");
170 MODULE_DESCRIPTION("DesignWare PWM Controller");
171 MODULE_LICENSE("GPL");