2017-11-07 13:58:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-11-13 14:14:13 +00:00
|
|
|
/*
|
|
|
|
* PWM Greybus driver.
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
* Copyright 2014 Linaro Ltd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/pwm.h>
|
2019-08-25 05:54:27 +00:00
|
|
|
#include <linux/greybus.h>
|
2015-05-22 17:35:31 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
#include "gbphy.h"
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
struct gb_pwm_chip {
|
|
|
|
struct gb_connection *connection;
|
|
|
|
u8 pwm_max; /* max pwm number */
|
|
|
|
|
|
|
|
struct pwm_chip chip;
|
|
|
|
struct pwm_chip *pwm;
|
|
|
|
};
|
|
|
|
#define pwm_chip_to_gb_pwm_chip(chip) \
|
|
|
|
container_of(chip, struct gb_pwm_chip, chip)
|
|
|
|
|
|
|
|
|
|
|
|
static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_count_response response;
|
2014-11-13 14:14:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
|
|
|
|
NULL, 0, &response, sizeof(response));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
pwmc->pwm_max = response.count;
|
|
|
|
return 0;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_activate_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_deactivate_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which, u32 duty, u32 period)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_config_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2014-12-02 21:48:09 +00:00
|
|
|
request.duty = cpu_to_le32(duty);
|
|
|
|
request.period = cpu_to_le32(period);
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which, u8 polarity)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_polarity_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
|
|
|
request.polarity = polarity;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_enable_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
if (ret)
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
|
|
|
|
u8 which)
|
|
|
|
{
|
2014-11-24 01:45:24 +00:00
|
|
|
struct gb_pwm_disable_request request;
|
2016-07-14 20:13:00 +00:00
|
|
|
struct gbphy_device *gbphy_dev;
|
|
|
|
int ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
if (which > pwmc->pwm_max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-11-24 01:45:24 +00:00
|
|
|
request.which = which;
|
2016-07-14 20:13:00 +00:00
|
|
|
|
|
|
|
ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
|
|
|
|
&request, sizeof(request), NULL, 0);
|
|
|
|
|
|
|
|
gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
|
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
|
|
|
|
|
|
|
return ret;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
|
|
|
return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
2016-06-08 04:33:54 +00:00
|
|
|
if (pwm_is_enabled(pwm))
|
2014-11-13 14:14:13 +00:00
|
|
|
dev_warn(chip->dev, "freeing PWM device without disabling\n");
|
|
|
|
|
|
|
|
gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
|
|
int duty_ns, int period_ns)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
|
|
|
return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns);
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
|
|
enum pwm_polarity polarity)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
|
|
|
return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity);
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
|
|
|
return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
|
|
|
|
|
|
|
|
gb_pwm_disable_operation(pwmc, pwm->hwpwm);
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct pwm_ops gb_pwm_ops = {
|
|
|
|
.request = gb_pwm_request,
|
|
|
|
.free = gb_pwm_free,
|
|
|
|
.config = gb_pwm_config,
|
|
|
|
.set_polarity = gb_pwm_set_polarity,
|
|
|
|
.enable = gb_pwm_enable,
|
|
|
|
.disable = gb_pwm_disable,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
|
|
|
|
const struct gbphy_device_id *id)
|
2014-11-13 14:14:13 +00:00
|
|
|
{
|
2016-05-05 09:02:31 +00:00
|
|
|
struct gb_connection *connection;
|
2014-11-13 14:14:13 +00:00
|
|
|
struct gb_pwm_chip *pwmc;
|
|
|
|
struct pwm_chip *pwm;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
|
|
|
|
if (!pwmc)
|
|
|
|
return -ENOMEM;
|
2016-05-05 09:02:31 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
connection = gb_connection_create(gbphy_dev->bundle,
|
|
|
|
le16_to_cpu(gbphy_dev->cport_desc->id),
|
2016-05-05 09:02:31 +00:00
|
|
|
NULL);
|
|
|
|
if (IS_ERR(connection)) {
|
|
|
|
ret = PTR_ERR(connection);
|
|
|
|
goto exit_pwmc_free;
|
|
|
|
}
|
|
|
|
|
2014-11-13 14:14:13 +00:00
|
|
|
pwmc->connection = connection;
|
2016-03-22 18:30:35 +00:00
|
|
|
gb_connection_set_data(connection, pwmc);
|
2016-05-19 15:52:39 +00:00
|
|
|
gb_gbphy_set_data(gbphy_dev, pwmc);
|
2016-05-05 09:02:31 +00:00
|
|
|
|
|
|
|
ret = gb_connection_enable(connection);
|
|
|
|
if (ret)
|
|
|
|
goto exit_connection_destroy;
|
|
|
|
|
2014-11-13 14:14:13 +00:00
|
|
|
/* Query number of pwms present */
|
|
|
|
ret = gb_pwm_count_operation(pwmc);
|
|
|
|
if (ret)
|
2016-05-05 09:02:31 +00:00
|
|
|
goto exit_connection_disable;
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
pwm = &pwmc->chip;
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
pwm->dev = &gbphy_dev->dev;
|
2014-11-13 14:14:13 +00:00
|
|
|
pwm->ops = &gb_pwm_ops;
|
|
|
|
pwm->base = -1; /* Allocate base dynamically */
|
|
|
|
pwm->npwm = pwmc->pwm_max + 1;
|
|
|
|
|
|
|
|
ret = pwmchip_add(pwm);
|
|
|
|
if (ret) {
|
2016-05-19 15:52:39 +00:00
|
|
|
dev_err(&gbphy_dev->dev,
|
2015-10-14 18:18:50 +00:00
|
|
|
"failed to register PWM: %d\n", ret);
|
2016-05-05 09:02:31 +00:00
|
|
|
goto exit_connection_disable;
|
2014-11-13 14:14:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
gbphy_runtime_put_autosuspend(gbphy_dev);
|
2014-11-13 14:14:13 +00:00
|
|
|
return 0;
|
2016-05-05 09:02:31 +00:00
|
|
|
|
|
|
|
exit_connection_disable:
|
|
|
|
gb_connection_disable(connection);
|
|
|
|
exit_connection_destroy:
|
|
|
|
gb_connection_destroy(connection);
|
|
|
|
exit_pwmc_free:
|
2014-11-13 14:14:13 +00:00
|
|
|
kfree(pwmc);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
|
2014-11-13 14:14:13 +00:00
|
|
|
{
|
2016-05-19 15:52:39 +00:00
|
|
|
struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
|
2016-05-05 09:02:31 +00:00
|
|
|
struct gb_connection *connection = pwmc->connection;
|
2016-07-14 20:13:00 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gbphy_runtime_get_sync(gbphy_dev);
|
|
|
|
if (ret)
|
|
|
|
gbphy_runtime_get_noresume(gbphy_dev);
|
2014-11-13 14:14:13 +00:00
|
|
|
|
|
|
|
pwmchip_remove(&pwmc->chip);
|
2016-05-05 09:02:31 +00:00
|
|
|
gb_connection_disable(connection);
|
|
|
|
gb_connection_destroy(connection);
|
2014-11-13 14:14:13 +00:00
|
|
|
kfree(pwmc);
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static const struct gbphy_device_id gb_pwm_id_table[] = {
|
|
|
|
{ GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
|
2016-05-05 09:02:31 +00:00
|
|
|
{ },
|
2014-11-13 14:14:13 +00:00
|
|
|
};
|
2016-05-19 15:52:39 +00:00
|
|
|
MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
|
2014-11-13 14:14:13 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
static struct gbphy_driver pwm_driver = {
|
2016-05-05 09:02:31 +00:00
|
|
|
.name = "pwm",
|
|
|
|
.probe = gb_pwm_probe,
|
|
|
|
.remove = gb_pwm_remove,
|
|
|
|
.id_table = gb_pwm_id_table,
|
|
|
|
};
|
2016-05-09 12:45:08 +00:00
|
|
|
|
2016-05-19 15:52:39 +00:00
|
|
|
module_gbphy_driver(pwm_driver);
|
2016-05-09 12:45:08 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|