0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/ctype.h>
0015 #include <linux/delay.h>
0016 #include <linux/export.h>
0017 #include <linux/gpio.h>
0018 #include <linux/gpio/driver.h>
0019 #include <linux/init.h>
0020 #include <linux/of_gpio.h>
0021 #include <linux/of.h>
0022 #include <linux/pinctrl/consumer.h>
0023 #include <linux/slab.h>
0024 #include <sound/ac97_codec.h>
0025 #include <sound/soc.h>
0026
0027 struct snd_ac97_reset_cfg {
0028 struct pinctrl *pctl;
0029 struct pinctrl_state *pstate_reset;
0030 struct pinctrl_state *pstate_warm_reset;
0031 struct pinctrl_state *pstate_run;
0032 int gpio_sdata;
0033 int gpio_sync;
0034 int gpio_reset;
0035 };
0036
0037 static struct snd_ac97_bus soc_ac97_bus = {
0038 .ops = NULL,
0039 };
0040
0041 static void soc_ac97_device_release(struct device *dev)
0042 {
0043 kfree(to_ac97_t(dev));
0044 }
0045
0046 #ifdef CONFIG_GPIOLIB
0047 struct snd_ac97_gpio_priv {
0048 struct gpio_chip gpio_chip;
0049 unsigned int gpios_set;
0050 struct snd_soc_component *component;
0051 };
0052
0053 static inline struct snd_soc_component *gpio_to_component(struct gpio_chip *chip)
0054 {
0055 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
0056
0057 return gpio_priv->component;
0058 }
0059
0060 static int snd_soc_ac97_gpio_request(struct gpio_chip *chip, unsigned offset)
0061 {
0062 if (offset >= AC97_NUM_GPIOS)
0063 return -EINVAL;
0064
0065 return 0;
0066 }
0067
0068 static int snd_soc_ac97_gpio_direction_in(struct gpio_chip *chip,
0069 unsigned offset)
0070 {
0071 struct snd_soc_component *component = gpio_to_component(chip);
0072
0073 dev_dbg(component->dev, "set gpio %d to output\n", offset);
0074 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
0075 1 << offset, 1 << offset);
0076 }
0077
0078 static int snd_soc_ac97_gpio_get(struct gpio_chip *chip, unsigned offset)
0079 {
0080 struct snd_soc_component *component = gpio_to_component(chip);
0081 int ret;
0082
0083 ret = snd_soc_component_read(component, AC97_GPIO_STATUS);
0084
0085 dev_dbg(component->dev, "get gpio %d : %d\n", offset,
0086 ret & (1 << offset));
0087
0088 return !!(ret & (1 << offset));
0089 }
0090
0091 static void snd_soc_ac97_gpio_set(struct gpio_chip *chip, unsigned offset,
0092 int value)
0093 {
0094 struct snd_ac97_gpio_priv *gpio_priv = gpiochip_get_data(chip);
0095 struct snd_soc_component *component = gpio_to_component(chip);
0096
0097 gpio_priv->gpios_set &= ~(1 << offset);
0098 gpio_priv->gpios_set |= (!!value) << offset;
0099 snd_soc_component_write(component, AC97_GPIO_STATUS,
0100 gpio_priv->gpios_set);
0101 dev_dbg(component->dev, "set gpio %d to %d\n", offset, !!value);
0102 }
0103
0104 static int snd_soc_ac97_gpio_direction_out(struct gpio_chip *chip,
0105 unsigned offset, int value)
0106 {
0107 struct snd_soc_component *component = gpio_to_component(chip);
0108
0109 dev_dbg(component->dev, "set gpio %d to output\n", offset);
0110 snd_soc_ac97_gpio_set(chip, offset, value);
0111 return snd_soc_component_update_bits(component, AC97_GPIO_CFG,
0112 1 << offset, 0);
0113 }
0114
0115 static const struct gpio_chip snd_soc_ac97_gpio_chip = {
0116 .label = "snd_soc_ac97",
0117 .owner = THIS_MODULE,
0118 .request = snd_soc_ac97_gpio_request,
0119 .direction_input = snd_soc_ac97_gpio_direction_in,
0120 .get = snd_soc_ac97_gpio_get,
0121 .direction_output = snd_soc_ac97_gpio_direction_out,
0122 .set = snd_soc_ac97_gpio_set,
0123 .can_sleep = 1,
0124 };
0125
0126 static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
0127 struct snd_soc_component *component)
0128 {
0129 struct snd_ac97_gpio_priv *gpio_priv;
0130 int ret;
0131
0132 gpio_priv = devm_kzalloc(component->dev, sizeof(*gpio_priv), GFP_KERNEL);
0133 if (!gpio_priv)
0134 return -ENOMEM;
0135 ac97->gpio_priv = gpio_priv;
0136 gpio_priv->component = component;
0137 gpio_priv->gpio_chip = snd_soc_ac97_gpio_chip;
0138 gpio_priv->gpio_chip.ngpio = AC97_NUM_GPIOS;
0139 gpio_priv->gpio_chip.parent = component->dev;
0140 gpio_priv->gpio_chip.base = -1;
0141
0142 ret = gpiochip_add_data(&gpio_priv->gpio_chip, gpio_priv);
0143 if (ret != 0)
0144 dev_err(component->dev, "Failed to add GPIOs: %d\n", ret);
0145 return ret;
0146 }
0147
0148 static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
0149 {
0150 gpiochip_remove(&ac97->gpio_priv->gpio_chip);
0151 }
0152 #else
0153 static int snd_soc_ac97_init_gpio(struct snd_ac97 *ac97,
0154 struct snd_soc_component *component)
0155 {
0156 return 0;
0157 }
0158
0159 static void snd_soc_ac97_free_gpio(struct snd_ac97 *ac97)
0160 {
0161 }
0162 #endif
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 struct snd_ac97 *snd_soc_alloc_ac97_component(struct snd_soc_component *component)
0175 {
0176 struct snd_ac97 *ac97;
0177
0178 ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
0179 if (ac97 == NULL)
0180 return ERR_PTR(-ENOMEM);
0181
0182 ac97->bus = &soc_ac97_bus;
0183 ac97->num = 0;
0184
0185 ac97->dev.bus = &ac97_bus_type;
0186 ac97->dev.parent = component->card->dev;
0187 ac97->dev.release = soc_ac97_device_release;
0188
0189 dev_set_name(&ac97->dev, "%d-%d:%s",
0190 component->card->snd_card->number, 0,
0191 component->name);
0192
0193 device_initialize(&ac97->dev);
0194
0195 return ac97;
0196 }
0197 EXPORT_SYMBOL(snd_soc_alloc_ac97_component);
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 struct snd_ac97 *snd_soc_new_ac97_component(struct snd_soc_component *component,
0214 unsigned int id, unsigned int id_mask)
0215 {
0216 struct snd_ac97 *ac97;
0217 int ret;
0218
0219 ac97 = snd_soc_alloc_ac97_component(component);
0220 if (IS_ERR(ac97))
0221 return ac97;
0222
0223 if (id) {
0224 ret = snd_ac97_reset(ac97, false, id, id_mask);
0225 if (ret < 0) {
0226 dev_err(component->dev, "Failed to reset AC97 device: %d\n",
0227 ret);
0228 goto err_put_device;
0229 }
0230 }
0231
0232 ret = device_add(&ac97->dev);
0233 if (ret)
0234 goto err_put_device;
0235
0236 ret = snd_soc_ac97_init_gpio(ac97, component);
0237 if (ret)
0238 goto err_put_device;
0239
0240 return ac97;
0241
0242 err_put_device:
0243 put_device(&ac97->dev);
0244 return ERR_PTR(ret);
0245 }
0246 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_component);
0247
0248
0249
0250
0251
0252
0253
0254 void snd_soc_free_ac97_component(struct snd_ac97 *ac97)
0255 {
0256 snd_soc_ac97_free_gpio(ac97);
0257 device_del(&ac97->dev);
0258 ac97->bus = NULL;
0259 put_device(&ac97->dev);
0260 }
0261 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_component);
0262
0263 static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
0264
0265 static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
0266 {
0267 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
0268
0269 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
0270
0271 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
0272
0273 udelay(10);
0274
0275 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
0276
0277 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
0278 msleep(2);
0279 }
0280
0281 static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
0282 {
0283 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
0284
0285 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
0286
0287 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
0288 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
0289 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
0290
0291 udelay(10);
0292
0293 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
0294
0295 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
0296 msleep(2);
0297 }
0298
0299 static int snd_soc_ac97_parse_pinctl(struct device *dev,
0300 struct snd_ac97_reset_cfg *cfg)
0301 {
0302 struct pinctrl *p;
0303 struct pinctrl_state *state;
0304 int gpio;
0305 int ret;
0306
0307 p = devm_pinctrl_get(dev);
0308 if (IS_ERR(p)) {
0309 dev_err(dev, "Failed to get pinctrl\n");
0310 return PTR_ERR(p);
0311 }
0312 cfg->pctl = p;
0313
0314 state = pinctrl_lookup_state(p, "ac97-reset");
0315 if (IS_ERR(state)) {
0316 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
0317 return PTR_ERR(state);
0318 }
0319 cfg->pstate_reset = state;
0320
0321 state = pinctrl_lookup_state(p, "ac97-warm-reset");
0322 if (IS_ERR(state)) {
0323 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
0324 return PTR_ERR(state);
0325 }
0326 cfg->pstate_warm_reset = state;
0327
0328 state = pinctrl_lookup_state(p, "ac97-running");
0329 if (IS_ERR(state)) {
0330 dev_err(dev, "Can't find pinctrl state ac97-running\n");
0331 return PTR_ERR(state);
0332 }
0333 cfg->pstate_run = state;
0334
0335 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
0336 if (gpio < 0) {
0337 dev_err(dev, "Can't find ac97-sync gpio\n");
0338 return gpio;
0339 }
0340 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
0341 if (ret) {
0342 dev_err(dev, "Failed requesting ac97-sync gpio\n");
0343 return ret;
0344 }
0345 cfg->gpio_sync = gpio;
0346
0347 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
0348 if (gpio < 0) {
0349 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
0350 return gpio;
0351 }
0352 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
0353 if (ret) {
0354 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
0355 return ret;
0356 }
0357 cfg->gpio_sdata = gpio;
0358
0359 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
0360 if (gpio < 0) {
0361 dev_err(dev, "Can't find ac97-reset gpio\n");
0362 return gpio;
0363 }
0364 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
0365 if (ret) {
0366 dev_err(dev, "Failed requesting ac97-reset gpio\n");
0367 return ret;
0368 }
0369 cfg->gpio_reset = gpio;
0370
0371 return 0;
0372 }
0373
0374 struct snd_ac97_bus_ops *soc_ac97_ops;
0375 EXPORT_SYMBOL_GPL(soc_ac97_ops);
0376
0377 int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
0378 {
0379 if (ops == soc_ac97_ops)
0380 return 0;
0381
0382 if (soc_ac97_ops && ops)
0383 return -EBUSY;
0384
0385 soc_ac97_ops = ops;
0386 soc_ac97_bus.ops = ops;
0387
0388 return 0;
0389 }
0390 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400 int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
0401 struct platform_device *pdev)
0402 {
0403 struct device *dev = &pdev->dev;
0404 struct snd_ac97_reset_cfg cfg;
0405 int ret;
0406
0407 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
0408 if (ret)
0409 return ret;
0410
0411 ret = snd_soc_set_ac97_ops(ops);
0412 if (ret)
0413 return ret;
0414
0415 ops->warm_reset = snd_soc_ac97_warm_reset;
0416 ops->reset = snd_soc_ac97_reset;
0417
0418 snd_ac97_rst_cfg = cfg;
0419 return 0;
0420 }
0421 EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);