summaryrefslogtreecommitdiffstats
path: root/kernel/drivers/pwm/pwm-clps711x.c
blob: a80c10803636adbd24d79865ee2a1207df90cd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Cirrus Logic CLPS711X PWM driver
 *
 * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/clk.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>

struct clps711x_chip {
	struct pwm_chip chip;
	void __iomem *pmpcon;
	struct clk *clk;
	spinlock_t lock;
};

static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip)
{
	return container_of(chip, struct clps711x_chip, chip);
}

static void clps711x_pwm_update_val(struct clps711x_chip *priv, u32 n, u32 v)
{
	/* PWM0 - bits 4..7, PWM1 - bits 8..11 */
	u32 shift = (n + 1) * 4;
	unsigned long flags;
	u32 tmp;

	spin_lock_irqsave(&priv->lock, flags);

	tmp = readl(priv->pmpcon);
	tmp &= ~(0xf << shift);
	tmp |= v << shift;
	writel(tmp, priv->pmpcon);

	spin_unlock_irqrestore(&priv->lock, flags);
}

static unsigned int clps711x_get_duty(struct pwm_device *pwm, unsigned int v)
{
	/* Duty cycle 0..15 max */
	return DIV_ROUND_CLOSEST(v * 0xf, pwm_get_period(pwm));
}

static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct clps711x_chip *priv = to_clps711x_chip(chip);
	unsigned int freq = clk_get_rate(priv->clk);

	if (!freq)
		return -EINVAL;

	/* Store constant period value */
	pwm_set_period(pwm, DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq));

	return 0;
}

static int clps711x_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
			       int duty_ns, int period_ns)
{
	struct clps711x_chip *priv = to_clps711x_chip(chip);
	unsigned int duty;

	if (period_ns != pwm_get_period(pwm))
		return -EINVAL;

	duty = clps711x_get_duty(pwm, duty_ns);
	clps711x_pwm_update_val(priv, pwm->hwpwm, duty);

	return 0;
}

static int clps711x_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct clps711x_chip *priv = to_clps711x_chip(chip);
	unsigned int duty;

	duty = clps711x_get_duty(pwm, pwm_get_duty_cycle(pwm));
	clps711x_pwm_update_val(priv, pwm->hwpwm, duty);

	return 0;
}

static void clps711x_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct clps711x_chip *priv = to_clps711x_chip(chip);

	clps711x_pwm_update_val(priv, pwm->hwpwm, 0);
}

static const struct pwm_ops clps711x_pwm_ops = {
	.request = clps711x_pwm_request,
	.config = clps711x_pwm_config,
	.enable = clps711x_pwm_enable,
	.disable = clps711x_pwm_disable,
	.owner = THIS_MODULE,
};

static struct pwm_device *clps711x_pwm_xlate(struct pwm_chip *chip,
					     const struct of_phandle_args *args)
{
	if (args->args[0] >= chip->npwm)
		return ERR_PTR(-EINVAL);

	return pwm_request_from_chip(chip, args->args[0], NULL);
}

static int clps711x_pwm_probe(struct platform_device *pdev)
{
	struct clps711x_chip *priv;
	struct resource *res;

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	priv->pmpcon = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(priv->pmpcon))
		return PTR_ERR(priv->pmpcon);

	priv->clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(priv->clk))
		return PTR_ERR(priv->clk);

	priv->chip.ops = &clps711x_pwm_ops;
	priv->chip.dev = &pdev->dev;
	priv->chip.base = -1;
	priv->chip.npwm = 2;
	priv->chip.of_xlate = clps711x_pwm_xlate;
	priv->chip.of_pwm_n_cells = 1;

	spin_lock_init(&priv->lock);

	platform_set_drvdata(pdev, priv);

	return pwmchip_add(&priv->chip);
}

static int clps711x_pwm_remove(struct platform_device *pdev)
{
	struct clps711x_chip *priv = platform_get_drvdata(pdev);

	return pwmchip_remove(&priv->chip);
}

static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
	{ .compatible = "cirrus,clps711x-pwm", },
	{ }
};
MODULE_DEVICE_TABLE(of, clps711x_pwm_dt_ids);

static struct platform_driver clps711x_pwm_driver = {
	.driver = {
		.name = "clps711x-pwm",
		.of_match_table = of_match_ptr(clps711x_pwm_dt_ids),
	},
	.probe = clps711x_pwm_probe,
	.remove = clps711x_pwm_remove,
};
module_platform_driver(clps711x_pwm_driver);

MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
MODULE_LICENSE("GPL");
an class="n">array_len, GFP_KERNEL); if (!nfsacl_desc->acl) return -ENOMEM; nfsacl_desc->count = 0; } entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++]; entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT; id = ntohl(*p++); entry->e_perm = ntohl(*p++); switch(entry->e_tag) { case ACL_USER: entry->e_uid = make_kuid(&init_user_ns, id); if (!uid_valid(entry->e_uid)) return -EINVAL; break; case ACL_GROUP: entry->e_gid = make_kgid(&init_user_ns, id); if (!gid_valid(entry->e_gid)) return -EINVAL; break; case ACL_USER_OBJ: case ACL_GROUP_OBJ: case ACL_OTHER: if (entry->e_perm & ~S_IRWXO) return -EINVAL; break; case ACL_MASK: /* Solaris sometimes sets additional bits in the mask */ entry->e_perm &= S_IRWXO; break; default: return -EINVAL; } return 0; } static int cmp_acl_entry(const void *x, const void *y) { const struct posix_acl_entry *a = x, *b = y; if (a->e_tag != b->e_tag) return a->e_tag - b->e_tag; else if ((a->e_tag == ACL_USER) && uid_gt(a->e_uid, b->e_uid)) return 1; else if ((a->e_tag == ACL_USER) && uid_lt(a->e_uid, b->e_uid)) return -1; else if ((a->e_tag == ACL_GROUP) && gid_gt(a->e_gid, b->e_gid)) return 1; else if ((a->e_tag == ACL_GROUP) && gid_lt(a->e_gid, b->e_gid)) return -1; else return 0; } /* * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL. */ static int posix_acl_from_nfsacl(struct posix_acl *acl) { struct posix_acl_entry *pa, *pe, *group_obj = NULL, *mask = NULL; if (!acl) return 0; sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry), cmp_acl_entry, NULL); /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */ FOREACH_ACL_ENTRY(pa, acl, pe) { switch(pa->e_tag) { case ACL_USER_OBJ: break; case ACL_GROUP_OBJ: group_obj = pa; break; case ACL_MASK: mask = pa; /* fall through */ case ACL_OTHER: break; } } if (acl->a_count == 4 && group_obj && mask && mask->e_perm == group_obj->e_perm) { /* remove bogus ACL_MASK entry */ memmove(mask, mask+1, (3 - (mask - acl->a_entries)) * sizeof(struct posix_acl_entry)); acl->a_count = 3; } return 0; } /** * nfsacl_decode - Decode an NFSv3 ACL * * @buf: xdr_buf containing XDR'd ACL data to decode * @base: byte offset in xdr_buf where XDR'd ACL begins * @aclcnt: count of ACEs in decoded posix_acl * @pacl: buffer in which to place decoded posix_acl * * Returns the length of the decoded ACL in bytes, or a negative errno value. */ int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt, struct posix_acl **pacl) { struct nfsacl_decode_desc nfsacl_desc = { .desc = { .elem_size = 12, .xcode = pacl ? xdr_nfsace_decode : NULL, }, }; u32 entries; int err; if (xdr_decode_word(buf, base, &entries) || entries > NFS_ACL_MAX_ENTRIES) return -EINVAL; nfsacl_desc.desc.array_maxlen = entries; err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc); if (err) return err; if (pacl) { if (entries != nfsacl_desc.desc.array_len || posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) { posix_acl_release(nfsacl_desc.acl); return -EINVAL; } *pacl = nfsacl_desc.acl; } if (aclcnt) *aclcnt = entries; return 8 + nfsacl_desc.desc.elem_size * nfsacl_desc.desc.array_len; } EXPORT_SYMBOL_GPL(nfsacl_decode);