4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/pwm.h>
19 struct gb_connection *connection;
20 u8 pwm_max; /* max pwm number */
25 #define pwm_chip_to_gb_pwm_chip(chip) \
26 container_of(chip, struct gb_pwm_chip, chip)
29 static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
31 struct gb_pwm_count_response response;
34 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
35 NULL, 0, &response, sizeof(response));
38 pwmc->pwm_max = response.count;
42 static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
45 struct gb_pwm_activate_request request;
47 if (which > pwmc->pwm_max)
50 request.which = which;
51 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
52 &request, sizeof(request), NULL, 0);
55 static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
58 struct gb_pwm_deactivate_request request;
60 if (which > pwmc->pwm_max)
63 request.which = which;
64 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
65 &request, sizeof(request), NULL, 0);
68 static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
69 u8 which, u32 duty, u32 period)
71 struct gb_pwm_config_request request;
73 if (which > pwmc->pwm_max)
76 request.which = which;
77 request.duty = cpu_to_le32(duty);
78 request.period = cpu_to_le32(period);
79 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
80 &request, sizeof(request), NULL, 0);
84 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
85 u8 which, u8 polarity)
87 struct gb_pwm_polarity_request request;
89 if (which > pwmc->pwm_max)
92 request.which = which;
93 request.polarity = polarity;
94 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
95 &request, sizeof(request), NULL, 0);
98 static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
101 struct gb_pwm_enable_request request;
103 if (which > pwmc->pwm_max)
106 request.which = which;
107 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
108 &request, sizeof(request), NULL, 0);
111 static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
114 struct gb_pwm_disable_request request;
116 if (which > pwmc->pwm_max)
119 request.which = which;
120 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
121 &request, sizeof(request), NULL, 0);
124 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
126 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
128 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
131 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
133 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
135 if (test_bit(PWMF_ENABLED, &pwm->flags))
136 dev_warn(chip->dev, "freeing PWM device without disabling\n");
138 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
141 static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
142 int duty_ns, int period_ns)
144 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
146 return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns);
149 static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
150 enum pwm_polarity polarity)
152 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
154 return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity);
157 static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
159 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
161 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
164 static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
166 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
168 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
171 static const struct pwm_ops gb_pwm_ops = {
172 .request = gb_pwm_request,
174 .config = gb_pwm_config,
175 .set_polarity = gb_pwm_set_polarity,
176 .enable = gb_pwm_enable,
177 .disable = gb_pwm_disable,
178 .owner = THIS_MODULE,
181 static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
182 const struct gbphy_device_id *id)
184 struct gb_connection *connection;
185 struct gb_pwm_chip *pwmc;
186 struct pwm_chip *pwm;
189 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
193 connection = gb_connection_create(gbphy_dev->bundle,
194 le16_to_cpu(gbphy_dev->cport_desc->id),
196 if (IS_ERR(connection)) {
197 ret = PTR_ERR(connection);
201 pwmc->connection = connection;
202 gb_connection_set_data(connection, pwmc);
203 gb_gbphy_set_data(gbphy_dev, pwmc);
205 ret = gb_connection_enable(connection);
207 goto exit_connection_destroy;
209 ret = gb_gbphy_get_version(connection);
211 goto exit_connection_disable;
213 /* Query number of pwms present */
214 ret = gb_pwm_count_operation(pwmc);
216 goto exit_connection_disable;
220 pwm->dev = &gbphy_dev->dev;
221 pwm->ops = &gb_pwm_ops;
222 pwm->base = -1; /* Allocate base dynamically */
223 pwm->npwm = pwmc->pwm_max + 1;
224 pwm->can_sleep = true; /* FIXME */
226 ret = pwmchip_add(pwm);
228 dev_err(&gbphy_dev->dev,
229 "failed to register PWM: %d\n", ret);
230 goto exit_connection_disable;
235 exit_connection_disable:
236 gb_connection_disable(connection);
237 exit_connection_destroy:
238 gb_connection_destroy(connection);
244 static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
246 struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
247 struct gb_connection *connection = pwmc->connection;
249 pwmchip_remove(&pwmc->chip);
250 gb_connection_disable(connection);
251 gb_connection_destroy(connection);
255 static const struct gbphy_device_id gb_pwm_id_table[] = {
256 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
259 MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
261 static struct gbphy_driver pwm_driver = {
263 .probe = gb_pwm_probe,
264 .remove = gb_pwm_remove,
265 .id_table = gb_pwm_id_table,
268 module_gbphy_driver(pwm_driver);
269 MODULE_LICENSE("GPL v2");