blob: 65db083e242b36f547887c731237dc32dd0e5aea [file] [log] [blame]
Kuninori Morimoto1a8f0a32018-07-02 06:26:27 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-ac97.c -- ALSA SoC Audio Layer AC97 support
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Author: Liam Girdwood <lrg@slimlogic.co.uk>
11// with code, comments and ideas from :-
12// Richard Purdie <richard@openedhand.com>
Lars-Peter Clausen336b8422014-11-10 22:41:46 +010013
14#include <linux/ctype.h>
15#include <linux/delay.h>
16#include <linux/export.h>
17#include <linux/gpio.h>
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010018#include <linux/gpio/driver.h>
Lars-Peter Clausen336b8422014-11-10 22:41:46 +010019#include <linux/init.h>
20#include <linux/of_gpio.h>
21#include <linux/of.h>
22#include <linux/pinctrl/consumer.h>
23#include <linux/slab.h>
24#include <sound/ac97_codec.h>
25#include <sound/soc.h>
26
27struct snd_ac97_reset_cfg {
28 struct pinctrl *pctl;
29 struct pinctrl_state *pstate_reset;
30 struct pinctrl_state *pstate_warm_reset;
31 struct pinctrl_state *pstate_run;
32 int gpio_sdata;
33 int gpio_sync;
34 int gpio_reset;
35};
36
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010037struct snd_ac97_gpio_priv {
38#ifdef CONFIG_GPIOLIB
39 struct gpio_chip gpio_chip;
40#endif
41 unsigned int gpios_set;
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000042 struct snd_soc_component *component;
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010043};
44
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +010045static struct snd_ac97_bus soc_ac97_bus = {
46 .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
47};
48
Lars-Peter Clausen336b8422014-11-10 22:41:46 +010049static void soc_ac97_device_release(struct device *dev)
50{
51 kfree(to_ac97_t(dev));
52}
53
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010054#ifdef CONFIG_GPIOLIB
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000055static inline struct snd_soc_component *gpio_to_component(struct gpio_chip *chip)
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010056{
Linus Walleijf7cb5122015-12-08 23:48:29 +010057 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010058
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000059 return gpio_priv->component;
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010060}
61
62static int snd_soc_ac97_gpio_request(struct gpio_chip *chip, unsigned offset)
63{
64 if (offset >= AC97_NUM_GPIOS)
65 return -EINVAL;
66
67 return 0;
68}
69
70static int snd_soc_ac97_gpio_direction_in(struct gpio_chip *chip,
71 unsigned offset)
72{
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000073 struct snd_soc_component *component = gpio_to_component(chip);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010074
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000075 dev_dbg(component->dev, "set gpio %d to output\n", offset);
76 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010077 1 << offset, 1 << offset);
78}
79
80static int snd_soc_ac97_gpio_get(struct gpio_chip *chip, unsigned offset)
81{
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000082 struct snd_soc_component *component = gpio_to_component(chip);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010083 int ret;
84
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +090085 ret = snd_soc_component_read(component, AC97_GPIO_STATUS);
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000086
87 dev_dbg(component->dev, "get gpio %d : %d\n", offset,
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +090088 ret & (1 << offset));
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010089
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +090090 return !!(ret & (1 << offset));
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010091}
92
93static void snd_soc_ac97_gpio_set(struct gpio_chip *chip, unsigned offset,
94 int value)
95{
Linus Walleijf7cb5122015-12-08 23:48:29 +010096 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
Kuninori Morimotoc95869e2018-01-29 02:58:25 +000097 struct snd_soc_component *component = gpio_to_component(chip);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010098
99 gpio_priv->gpios_set &= ~(1 << offset);
100 gpio_priv->gpios_set |= (!!value) << offset;
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000101 snd_soc_component_write(component, AC97_GPIO_STATUS,
102 gpio_priv->gpios_set);
103 dev_dbg(component->dev, "set gpio %d to %d\n", offset, !!value);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100104}
105
106static int snd_soc_ac97_gpio_direction_out(struct gpio_chip *chip,
107 unsigned offset, int value)
108{
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000109 struct snd_soc_component *component = gpio_to_component(chip);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100110
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000111 dev_dbg(component->dev, "set gpio %d to output\n", offset);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100112 snd_soc_ac97_gpio_set(chip, offset, value);
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000113 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
114 1 << offset, 0);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100115}
116
Julia Lawall52abe542016-09-11 14:14:41 +0200117static const struct gpio_chip snd_soc_ac97_gpio_chip = {
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100118 .label = "snd_soc_ac97",
119 .owner = THIS_MODULE,
120 .request = snd_soc_ac97_gpio_request,
121 .direction_input = snd_soc_ac97_gpio_direction_in,
122 .get = snd_soc_ac97_gpio_get,
123 .direction_output = snd_soc_ac97_gpio_direction_out,
124 .set = snd_soc_ac97_gpio_set,
125 .can_sleep = 1,
126};
127
128static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000129 struct snd_soc_component *component)
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100130{
131 struct snd_ac97_gpio_priv *gpio_priv;
132 int ret;
133
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000134 gpio_priv = devm_kzalloc(component->dev, sizeof(*gpio_priv), GFP_KERNEL);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100135 if (!gpio_priv)
136 return -ENOMEM;
137 ac97->gpio_priv = gpio_priv;
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000138 gpio_priv->component = component;
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100139 gpio_priv->gpio_chip = snd_soc_ac97_gpio_chip;
140 gpio_priv->gpio_chip.ngpio = AC97_NUM_GPIOS;
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000141 gpio_priv->gpio_chip.parent = component->dev;
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100142 gpio_priv->gpio_chip.base = -1;
143
Linus Walleijf7cb5122015-12-08 23:48:29 +0100144 ret = gpiochip_add_data(&gpio_priv->gpio_chip, gpio_priv);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100145 if (ret != 0)
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000146 dev_err(component->dev, "Failed to add GPIOs: %d\n", ret);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100147 return ret;
148}
149
150static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
151{
152 gpiochip_remove(&ac97->gpio_priv->gpio_chip);
153}
154#else
155static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000156 struct snd_soc_component *component)
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100157{
158 return 0;
159}
160
161static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
162{
163}
164#endif
165
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100166/**
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000167 * snd_soc_alloc_ac97_component() - Allocate new a AC'97 device
168 * @component: The COMPONENT for which to create the AC'97 device
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100169 *
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100170 * Allocated a new snd_ac97 device and intializes it, but does not yet register
171 * it. The caller is responsible to either call device_add(&ac97->dev) to
172 * register the device, or to call put_device(&ac97->dev) to free the device.
173 *
174 * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100175 */
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000176struct snd_ac97 *snd_soc_alloc_ac97_component(struct snd_soc_component *component)
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100177{
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100178 struct snd_ac97 *ac97;
Lars-Peter Clausen6794f702014-11-10 22:41:50 +0100179
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100180 ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
181 if (ac97 == NULL)
182 return ERR_PTR(-ENOMEM);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100183
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100184 ac97->bus = &soc_ac97_bus;
185 ac97->num = 0;
Lars-Peter Clausen6794f702014-11-10 22:41:50 +0100186
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100187 ac97->dev.bus = &ac97_bus_type;
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000188 ac97->dev.parent = component->card->dev;
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100189 ac97->dev.release = soc_ac97_device_release;
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100190
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100191 dev_set_name(&ac97->dev, "%d-%d:%s",
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000192 component->card->snd_card->number, 0,
193 component->name);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100194
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100195 device_initialize(&ac97->dev);
196
197 return ac97;
198}
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000199EXPORT_SYMBOL(snd_soc_alloc_ac97_component);
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100200
201/**
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000202 * snd_soc_new_ac97_component - initailise AC97 device
203 * @component: audio component
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200204 * @id: The expected device ID
205 * @id_mask: Mask that is applied to the device ID before comparing with @id
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100206 *
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000207 * Initialises AC97 component resources for use by ad-hoc devices only.
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200208 *
209 * If @id is not 0 this function will reset the device, then read the ID from
210 * the device and check if it matches the expected ID. If it doesn't match an
211 * error will be returned and device will not be registered.
212 *
213 * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100214 */
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000215struct snd_ac97 *snd_soc_new_ac97_component(struct snd_soc_component *component,
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200216 unsigned int id, unsigned int id_mask)
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100217{
218 struct snd_ac97 *ac97;
219 int ret;
220
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000221 ac97 = snd_soc_alloc_ac97_component(component);
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100222 if (IS_ERR(ac97))
223 return ac97;
224
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200225 if (id) {
226 ret = snd_ac97_reset(ac97, false, id, id_mask);
227 if (ret < 0) {
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000228 dev_err(component->dev, "Failed to reset AC97 device: %d\n",
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200229 ret);
230 goto err_put_device;
231 }
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100232 }
Lars-Peter Clausen6794f702014-11-10 22:41:50 +0100233
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200234 ret = device_add(&ac97->dev);
235 if (ret)
236 goto err_put_device;
237
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000238 ret = snd_soc_ac97_init_gpio(ac97, component);
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100239 if (ret)
240 goto err_put_device;
241
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100242 return ac97;
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200243
244err_put_device:
245 put_device(&ac97->dev);
246 return ERR_PTR(ret);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100247}
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000248EXPORT_SYMBOL_GPL(snd_soc_new_ac97_component);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100249
250/**
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000251 * snd_soc_free_ac97_component - free AC97 component device
Charles Keepax8abab352017-01-12 11:38:15 +0000252 * @ac97: snd_ac97 device to be freed
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100253 *
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000254 * Frees AC97 component device resources.
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100255 */
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000256void snd_soc_free_ac97_component(struct snd_ac97 *ac97)
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100257{
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100258 snd_soc_ac97_free_gpio(ac97);
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100259 device_del(&ac97->dev);
260 ac97->bus = NULL;
261 put_device(&ac97->dev);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100262}
Kuninori Morimotoc95869e2018-01-29 02:58:25 +0000263EXPORT_SYMBOL_GPL(snd_soc_free_ac97_component);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100264
265static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
266
267static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
268{
269 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
270
271 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
272
273 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
274
275 udelay(10);
276
277 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
278
279 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
280 msleep(2);
281}
282
283static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
284{
285 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
286
287 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
288
289 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
290 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
291 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
292
293 udelay(10);
294
295 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
296
297 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
298 msleep(2);
299}
300
301static int snd_soc_ac97_parse_pinctl(struct device *dev,
302 struct snd_ac97_reset_cfg *cfg)
303{
304 struct pinctrl *p;
305 struct pinctrl_state *state;
306 int gpio;
307 int ret;
308
309 p = devm_pinctrl_get(dev);
310 if (IS_ERR(p)) {
311 dev_err(dev, "Failed to get pinctrl\n");
312 return PTR_ERR(p);
313 }
314 cfg->pctl = p;
315
316 state = pinctrl_lookup_state(p, "ac97-reset");
317 if (IS_ERR(state)) {
318 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
319 return PTR_ERR(state);
320 }
321 cfg->pstate_reset = state;
322
323 state = pinctrl_lookup_state(p, "ac97-warm-reset");
324 if (IS_ERR(state)) {
325 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
326 return PTR_ERR(state);
327 }
328 cfg->pstate_warm_reset = state;
329
330 state = pinctrl_lookup_state(p, "ac97-running");
331 if (IS_ERR(state)) {
332 dev_err(dev, "Can't find pinctrl state ac97-running\n");
333 return PTR_ERR(state);
334 }
335 cfg->pstate_run = state;
336
337 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
338 if (gpio < 0) {
339 dev_err(dev, "Can't find ac97-sync gpio\n");
340 return gpio;
341 }
342 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
343 if (ret) {
344 dev_err(dev, "Failed requesting ac97-sync gpio\n");
345 return ret;
346 }
347 cfg->gpio_sync = gpio;
348
349 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
350 if (gpio < 0) {
351 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
352 return gpio;
353 }
354 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
355 if (ret) {
356 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
357 return ret;
358 }
359 cfg->gpio_sdata = gpio;
360
361 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
362 if (gpio < 0) {
363 dev_err(dev, "Can't find ac97-reset gpio\n");
364 return gpio;
365 }
366 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
367 if (ret) {
368 dev_err(dev, "Failed requesting ac97-reset gpio\n");
369 return ret;
370 }
371 cfg->gpio_reset = gpio;
372
373 return 0;
374}
375
376struct snd_ac97_bus_ops *soc_ac97_ops;
377EXPORT_SYMBOL_GPL(soc_ac97_ops);
378
379int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
380{
381 if (ops == soc_ac97_ops)
382 return 0;
383
384 if (soc_ac97_ops && ops)
385 return -EBUSY;
386
387 soc_ac97_ops = ops;
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +0100388 soc_ac97_bus.ops = ops;
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100389
390 return 0;
391}
392EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
393
394/**
395 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
Pierre-Louis Bossart8182fa92020-07-02 12:28:00 -0500396 * @ops: bus ops
397 * @pdev: platform device
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100398 *
399 * This function sets the reset and warm_reset properties of ops and parses
400 * the device node of pdev to get pinctrl states and gpio numbers to use.
401 */
402int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
403 struct platform_device *pdev)
404{
405 struct device *dev = &pdev->dev;
406 struct snd_ac97_reset_cfg cfg;
407 int ret;
408
409 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
410 if (ret)
411 return ret;
412
413 ret = snd_soc_set_ac97_ops(ops);
414 if (ret)
415 return ret;
416
417 ops->warm_reset = snd_soc_ac97_warm_reset;
418 ops->reset = snd_soc_ac97_reset;
419
420 snd_ac97_rst_cfg = cfg;
421 return 0;
422}
423EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);