blob: ae563e379a7244410882adaa8b67e6a34298f663 [file] [log] [blame]
Lars-Peter Clausen336b8422014-11-10 22:41:46 +01001/*
2 * soc-ac97.c -- ALSA SoC Audio Layer AC97 support
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 */
18
19#include <linux/ctype.h>
20#include <linux/delay.h>
21#include <linux/export.h>
22#include <linux/gpio.h>
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010023#include <linux/gpio/driver.h>
Lars-Peter Clausen336b8422014-11-10 22:41:46 +010024#include <linux/init.h>
25#include <linux/of_gpio.h>
26#include <linux/of.h>
27#include <linux/pinctrl/consumer.h>
28#include <linux/slab.h>
29#include <sound/ac97_codec.h>
30#include <sound/soc.h>
31
32struct snd_ac97_reset_cfg {
33 struct pinctrl *pctl;
34 struct pinctrl_state *pstate_reset;
35 struct pinctrl_state *pstate_warm_reset;
36 struct pinctrl_state *pstate_run;
37 int gpio_sdata;
38 int gpio_sync;
39 int gpio_reset;
40};
41
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010042struct snd_ac97_gpio_priv {
43#ifdef CONFIG_GPIOLIB
44 struct gpio_chip gpio_chip;
45#endif
46 unsigned int gpios_set;
47 struct snd_soc_codec *codec;
48};
49
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +010050static struct snd_ac97_bus soc_ac97_bus = {
51 .ops = NULL, /* Gets initialized in snd_soc_set_ac97_ops() */
52};
53
Lars-Peter Clausen336b8422014-11-10 22:41:46 +010054static void soc_ac97_device_release(struct device *dev)
55{
56 kfree(to_ac97_t(dev));
57}
58
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +010059#ifdef CONFIG_GPIOLIB
60static inline struct snd_soc_codec *gpio_to_codec(struct gpio_chip *chip)
61{
62 struct snd_ac97_gpio_priv *gpio_priv =
63 container_of(chip, struct snd_ac97_gpio_priv, gpio_chip);
64
65 return gpio_priv->codec;
66}
67
68static int snd_soc_ac97_gpio_request(struct gpio_chip *chip, unsigned offset)
69{
70 if (offset >= AC97_NUM_GPIOS)
71 return -EINVAL;
72
73 return 0;
74}
75
76static int snd_soc_ac97_gpio_direction_in(struct gpio_chip *chip,
77 unsigned offset)
78{
79 struct snd_soc_codec *codec = gpio_to_codec(chip);
80
81 dev_dbg(codec->dev, "set gpio %d to output\n", offset);
82 return snd_soc_update_bits(codec, AC97_GPIO_CFG,
83 1 << offset, 1 << offset);
84}
85
86static int snd_soc_ac97_gpio_get(struct gpio_chip *chip, unsigned offset)
87{
88 struct snd_soc_codec *codec = gpio_to_codec(chip);
89 int ret;
90
91 ret = snd_soc_read(codec, AC97_GPIO_STATUS);
92 dev_dbg(codec->dev, "get gpio %d : %d\n", offset,
93 ret < 0 ? ret : ret & (1 << offset));
94
95 return ret < 0 ? ret : ret & (1 << offset);
96}
97
98static void snd_soc_ac97_gpio_set(struct gpio_chip *chip, unsigned offset,
99 int value)
100{
101 struct snd_ac97_gpio_priv *gpio_priv =
102 container_of(chip, struct snd_ac97_gpio_priv, gpio_chip);
103 struct snd_soc_codec *codec = gpio_to_codec(chip);
104
105 gpio_priv->gpios_set &= ~(1 << offset);
106 gpio_priv->gpios_set |= (!!value) << offset;
107 snd_soc_write(codec, AC97_GPIO_STATUS, gpio_priv->gpios_set);
108 dev_dbg(codec->dev, "set gpio %d to %d\n", offset, !!value);
109}
110
111static int snd_soc_ac97_gpio_direction_out(struct gpio_chip *chip,
112 unsigned offset, int value)
113{
114 struct snd_soc_codec *codec = gpio_to_codec(chip);
115
116 dev_dbg(codec->dev, "set gpio %d to output\n", offset);
117 snd_soc_ac97_gpio_set(chip, offset, value);
118 return snd_soc_update_bits(codec, AC97_GPIO_CFG, 1 << offset, 0);
119}
120
121static struct gpio_chip snd_soc_ac97_gpio_chip = {
122 .label = "snd_soc_ac97",
123 .owner = THIS_MODULE,
124 .request = snd_soc_ac97_gpio_request,
125 .direction_input = snd_soc_ac97_gpio_direction_in,
126 .get = snd_soc_ac97_gpio_get,
127 .direction_output = snd_soc_ac97_gpio_direction_out,
128 .set = snd_soc_ac97_gpio_set,
129 .can_sleep = 1,
130};
131
132static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
133 struct snd_soc_codec *codec)
134{
135 struct snd_ac97_gpio_priv *gpio_priv;
136 int ret;
137
138 gpio_priv = devm_kzalloc(codec->dev, sizeof(*gpio_priv), GFP_KERNEL);
139 if (!gpio_priv)
140 return -ENOMEM;
141 ac97->gpio_priv = gpio_priv;
142 gpio_priv->codec = codec;
143 gpio_priv->gpio_chip = snd_soc_ac97_gpio_chip;
144 gpio_priv->gpio_chip.ngpio = AC97_NUM_GPIOS;
145 gpio_priv->gpio_chip.dev = codec->dev;
146 gpio_priv->gpio_chip.base = -1;
147
148 ret = gpiochip_add(&gpio_priv->gpio_chip);
149 if (ret != 0)
150 dev_err(codec->dev, "Failed to add GPIOs: %d\n", ret);
151 return ret;
152}
153
154static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
155{
156 gpiochip_remove(&ac97->gpio_priv->gpio_chip);
157}
158#else
159static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
160 struct snd_soc_codec *codec)
161{
162 return 0;
163}
164
165static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
166{
167}
168#endif
169
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100170/**
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100171 * snd_soc_alloc_ac97_codec() - Allocate new a AC'97 device
172 * @codec: The CODEC for which to create the AC'97 device
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100173 *
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100174 * Allocated a new snd_ac97 device and intializes it, but does not yet register
175 * it. The caller is responsible to either call device_add(&ac97->dev) to
176 * register the device, or to call put_device(&ac97->dev) to free the device.
177 *
178 * Returns: A snd_ac97 device or a PTR_ERR in case of an error.
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100179 */
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100180struct snd_ac97 *snd_soc_alloc_ac97_codec(struct snd_soc_codec *codec)
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100181{
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100182 struct snd_ac97 *ac97;
Lars-Peter Clausen6794f702014-11-10 22:41:50 +0100183
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100184 ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
185 if (ac97 == NULL)
186 return ERR_PTR(-ENOMEM);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100187
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100188 ac97->bus = &soc_ac97_bus;
189 ac97->num = 0;
Lars-Peter Clausen6794f702014-11-10 22:41:50 +0100190
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100191 ac97->dev.bus = &ac97_bus_type;
192 ac97->dev.parent = codec->component.card->dev;
193 ac97->dev.release = soc_ac97_device_release;
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100194
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100195 dev_set_name(&ac97->dev, "%d-%d:%s",
Lars-Peter Clausen6794f702014-11-10 22:41:50 +0100196 codec->component.card->snd_card->number, 0,
197 codec->component.name);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100198
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100199 device_initialize(&ac97->dev);
200
201 return ac97;
202}
203EXPORT_SYMBOL(snd_soc_alloc_ac97_codec);
204
205/**
206 * snd_soc_new_ac97_codec - initailise AC97 device
207 * @codec: audio codec
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200208 * @id: The expected device ID
209 * @id_mask: Mask that is applied to the device ID before comparing with @id
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100210 *
211 * Initialises AC97 codec resources for use by ad-hoc devices only.
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200212 *
213 * If @id is not 0 this function will reset the device, then read the ID from
214 * the device and check if it matches the expected ID. If it doesn't match an
215 * error will be returned and device will not be registered.
216 *
217 * Returns: A PTR_ERR() on failure or a valid snd_ac97 struct on success.
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100218 */
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200219struct snd_ac97 *snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
220 unsigned int id, unsigned int id_mask)
Lars-Peter Clausen47e03942015-01-23 16:21:36 +0100221{
222 struct snd_ac97 *ac97;
223 int ret;
224
225 ac97 = snd_soc_alloc_ac97_codec(codec);
226 if (IS_ERR(ac97))
227 return ac97;
228
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200229 if (id) {
230 ret = snd_ac97_reset(ac97, false, id, id_mask);
231 if (ret < 0) {
232 dev_err(codec->dev, "Failed to reset AC97 device: %d\n",
233 ret);
234 goto err_put_device;
235 }
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100236 }
Lars-Peter Clausen6794f702014-11-10 22:41:50 +0100237
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200238 ret = device_add(&ac97->dev);
239 if (ret)
240 goto err_put_device;
241
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100242 ret = snd_soc_ac97_init_gpio(ac97, codec);
243 if (ret)
244 goto err_put_device;
245
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100246 return ac97;
Lars-Peter Clausen7361fbe2015-07-21 21:53:01 +0200247
248err_put_device:
249 put_device(&ac97->dev);
250 return ERR_PTR(ret);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100251}
252EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
253
254/**
255 * snd_soc_free_ac97_codec - free AC97 codec device
256 * @codec: audio codec
257 *
258 * Frees AC97 codec device resources.
259 */
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100260void snd_soc_free_ac97_codec(struct snd_ac97 *ac97)
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100261{
Robert Jarzmik9bf5c3d2015-11-11 13:12:51 +0100262 snd_soc_ac97_free_gpio(ac97);
Lars-Peter Clausen358a8bb2014-11-10 22:41:53 +0100263 device_del(&ac97->dev);
264 ac97->bus = NULL;
265 put_device(&ac97->dev);
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100266}
267EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
268
269static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
270
271static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
272{
273 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
274
275 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
276
277 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
278
279 udelay(10);
280
281 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
282
283 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
284 msleep(2);
285}
286
287static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
288{
289 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
290
291 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
292
293 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
294 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
295 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
296
297 udelay(10);
298
299 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
300
301 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
302 msleep(2);
303}
304
305static int snd_soc_ac97_parse_pinctl(struct device *dev,
306 struct snd_ac97_reset_cfg *cfg)
307{
308 struct pinctrl *p;
309 struct pinctrl_state *state;
310 int gpio;
311 int ret;
312
313 p = devm_pinctrl_get(dev);
314 if (IS_ERR(p)) {
315 dev_err(dev, "Failed to get pinctrl\n");
316 return PTR_ERR(p);
317 }
318 cfg->pctl = p;
319
320 state = pinctrl_lookup_state(p, "ac97-reset");
321 if (IS_ERR(state)) {
322 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
323 return PTR_ERR(state);
324 }
325 cfg->pstate_reset = state;
326
327 state = pinctrl_lookup_state(p, "ac97-warm-reset");
328 if (IS_ERR(state)) {
329 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
330 return PTR_ERR(state);
331 }
332 cfg->pstate_warm_reset = state;
333
334 state = pinctrl_lookup_state(p, "ac97-running");
335 if (IS_ERR(state)) {
336 dev_err(dev, "Can't find pinctrl state ac97-running\n");
337 return PTR_ERR(state);
338 }
339 cfg->pstate_run = state;
340
341 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
342 if (gpio < 0) {
343 dev_err(dev, "Can't find ac97-sync gpio\n");
344 return gpio;
345 }
346 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
347 if (ret) {
348 dev_err(dev, "Failed requesting ac97-sync gpio\n");
349 return ret;
350 }
351 cfg->gpio_sync = gpio;
352
353 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
354 if (gpio < 0) {
355 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
356 return gpio;
357 }
358 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
359 if (ret) {
360 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
361 return ret;
362 }
363 cfg->gpio_sdata = gpio;
364
365 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
366 if (gpio < 0) {
367 dev_err(dev, "Can't find ac97-reset gpio\n");
368 return gpio;
369 }
370 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
371 if (ret) {
372 dev_err(dev, "Failed requesting ac97-reset gpio\n");
373 return ret;
374 }
375 cfg->gpio_reset = gpio;
376
377 return 0;
378}
379
380struct snd_ac97_bus_ops *soc_ac97_ops;
381EXPORT_SYMBOL_GPL(soc_ac97_ops);
382
383int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
384{
385 if (ops == soc_ac97_ops)
386 return 0;
387
388 if (soc_ac97_ops && ops)
389 return -EBUSY;
390
391 soc_ac97_ops = ops;
Lars-Peter Clauseneda1a702014-11-10 22:41:47 +0100392 soc_ac97_bus.ops = ops;
Lars-Peter Clausen336b8422014-11-10 22:41:46 +0100393
394 return 0;
395}
396EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
397
398/**
399 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
400 *
401 * This function sets the reset and warm_reset properties of ops and parses
402 * the device node of pdev to get pinctrl states and gpio numbers to use.
403 */
404int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
405 struct platform_device *pdev)
406{
407 struct device *dev = &pdev->dev;
408 struct snd_ac97_reset_cfg cfg;
409 int ret;
410
411 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
412 if (ret)
413 return ret;
414
415 ret = snd_soc_set_ac97_ops(ops);
416 if (ret)
417 return ret;
418
419 ops->warm_reset = snd_soc_ac97_warm_reset;
420 ops->reset = snd_soc_ac97_reset;
421
422 snd_ac97_rst_cfg = cfg;
423 return 0;
424}
425EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);