xref: /openbmc/linux/drivers/mfd/wm8994-core.c (revision 9c1f8594)
1 /*
2  * wm8994-core.c  --  Device access for Wolfson WM8994
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/mfd/core.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/machine.h>
24 
25 #include <linux/mfd/wm8994/core.h>
26 #include <linux/mfd/wm8994/pdata.h>
27 #include <linux/mfd/wm8994/registers.h>
28 
29 static int wm8994_read(struct wm8994 *wm8994, unsigned short reg,
30 		       int bytes, void *dest)
31 {
32 	int ret, i;
33 	u16 *buf = dest;
34 
35 	BUG_ON(bytes % 2);
36 	BUG_ON(bytes <= 0);
37 
38 	ret = wm8994->read_dev(wm8994, reg, bytes, dest);
39 	if (ret < 0)
40 		return ret;
41 
42 	for (i = 0; i < bytes / 2; i++) {
43 		dev_vdbg(wm8994->dev, "Read %04x from R%d(0x%x)\n",
44 			 be16_to_cpu(buf[i]), reg + i, reg + i);
45 	}
46 
47 	return 0;
48 }
49 
50 /**
51  * wm8994_reg_read: Read a single WM8994 register.
52  *
53  * @wm8994: Device to read from.
54  * @reg: Register to read.
55  */
56 int wm8994_reg_read(struct wm8994 *wm8994, unsigned short reg)
57 {
58 	unsigned short val;
59 	int ret;
60 
61 	mutex_lock(&wm8994->io_lock);
62 
63 	ret = wm8994_read(wm8994, reg, 2, &val);
64 
65 	mutex_unlock(&wm8994->io_lock);
66 
67 	if (ret < 0)
68 		return ret;
69 	else
70 		return be16_to_cpu(val);
71 }
72 EXPORT_SYMBOL_GPL(wm8994_reg_read);
73 
74 /**
75  * wm8994_bulk_read: Read multiple WM8994 registers
76  *
77  * @wm8994: Device to read from
78  * @reg: First register
79  * @count: Number of registers
80  * @buf: Buffer to fill.  The data will be returned big endian.
81  */
82 int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg,
83 		     int count, u16 *buf)
84 {
85 	int ret;
86 
87 	mutex_lock(&wm8994->io_lock);
88 
89 	ret = wm8994_read(wm8994, reg, count * 2, buf);
90 
91 	mutex_unlock(&wm8994->io_lock);
92 
93 	return ret;
94 }
95 EXPORT_SYMBOL_GPL(wm8994_bulk_read);
96 
97 static int wm8994_write(struct wm8994 *wm8994, unsigned short reg,
98 			int bytes, const void *src)
99 {
100 	const u16 *buf = src;
101 	int i;
102 
103 	BUG_ON(bytes % 2);
104 	BUG_ON(bytes <= 0);
105 
106 	for (i = 0; i < bytes / 2; i++) {
107 		dev_vdbg(wm8994->dev, "Write %04x to R%d(0x%x)\n",
108 			 be16_to_cpu(buf[i]), reg + i, reg + i);
109 	}
110 
111 	return wm8994->write_dev(wm8994, reg, bytes, src);
112 }
113 
114 /**
115  * wm8994_reg_write: Write a single WM8994 register.
116  *
117  * @wm8994: Device to write to.
118  * @reg: Register to write to.
119  * @val: Value to write.
120  */
121 int wm8994_reg_write(struct wm8994 *wm8994, unsigned short reg,
122 		     unsigned short val)
123 {
124 	int ret;
125 
126 	val = cpu_to_be16(val);
127 
128 	mutex_lock(&wm8994->io_lock);
129 
130 	ret = wm8994_write(wm8994, reg, 2, &val);
131 
132 	mutex_unlock(&wm8994->io_lock);
133 
134 	return ret;
135 }
136 EXPORT_SYMBOL_GPL(wm8994_reg_write);
137 
138 /**
139  * wm8994_bulk_write: Write multiple WM8994 registers
140  *
141  * @wm8994: Device to write to
142  * @reg: First register
143  * @count: Number of registers
144  * @buf: Buffer to write from.  Data must be big-endian formatted.
145  */
146 int wm8994_bulk_write(struct wm8994 *wm8994, unsigned short reg,
147 		      int count, const u16 *buf)
148 {
149 	int ret;
150 
151 	mutex_lock(&wm8994->io_lock);
152 
153 	ret = wm8994_write(wm8994, reg, count * 2, buf);
154 
155 	mutex_unlock(&wm8994->io_lock);
156 
157 	return ret;
158 }
159 EXPORT_SYMBOL_GPL(wm8994_bulk_write);
160 
161 /**
162  * wm8994_set_bits: Set the value of a bitfield in a WM8994 register
163  *
164  * @wm8994: Device to write to.
165  * @reg: Register to write to.
166  * @mask: Mask of bits to set.
167  * @val: Value to set (unshifted)
168  */
169 int wm8994_set_bits(struct wm8994 *wm8994, unsigned short reg,
170 		    unsigned short mask, unsigned short val)
171 {
172 	int ret;
173 	u16 r;
174 
175 	mutex_lock(&wm8994->io_lock);
176 
177 	ret = wm8994_read(wm8994, reg, 2, &r);
178 	if (ret < 0)
179 		goto out;
180 
181 	r = be16_to_cpu(r);
182 
183 	r &= ~mask;
184 	r |= val;
185 
186 	r = cpu_to_be16(r);
187 
188 	ret = wm8994_write(wm8994, reg, 2, &r);
189 
190 out:
191 	mutex_unlock(&wm8994->io_lock);
192 
193 	return ret;
194 }
195 EXPORT_SYMBOL_GPL(wm8994_set_bits);
196 
197 static struct mfd_cell wm8994_regulator_devs[] = {
198 	{
199 		.name = "wm8994-ldo",
200 		.id = 1,
201 		.pm_runtime_no_callbacks = true,
202 	},
203 	{
204 		.name = "wm8994-ldo",
205 		.id = 2,
206 		.pm_runtime_no_callbacks = true,
207 	},
208 };
209 
210 static struct resource wm8994_codec_resources[] = {
211 	{
212 		.start = WM8994_IRQ_TEMP_SHUT,
213 		.end   = WM8994_IRQ_TEMP_WARN,
214 		.flags = IORESOURCE_IRQ,
215 	},
216 };
217 
218 static struct resource wm8994_gpio_resources[] = {
219 	{
220 		.start = WM8994_IRQ_GPIO(1),
221 		.end   = WM8994_IRQ_GPIO(11),
222 		.flags = IORESOURCE_IRQ,
223 	},
224 };
225 
226 static struct mfd_cell wm8994_devs[] = {
227 	{
228 		.name = "wm8994-codec",
229 		.num_resources = ARRAY_SIZE(wm8994_codec_resources),
230 		.resources = wm8994_codec_resources,
231 	},
232 
233 	{
234 		.name = "wm8994-gpio",
235 		.num_resources = ARRAY_SIZE(wm8994_gpio_resources),
236 		.resources = wm8994_gpio_resources,
237 		.pm_runtime_no_callbacks = true,
238 	},
239 };
240 
241 /*
242  * Supplies for the main bulk of CODEC; the LDO supplies are ignored
243  * and should be handled via the standard regulator API supply
244  * management.
245  */
246 static const char *wm8994_main_supplies[] = {
247 	"DBVDD",
248 	"DCVDD",
249 	"AVDD1",
250 	"AVDD2",
251 	"CPVDD",
252 	"SPKVDD1",
253 	"SPKVDD2",
254 };
255 
256 static const char *wm8958_main_supplies[] = {
257 	"DBVDD1",
258 	"DBVDD2",
259 	"DBVDD3",
260 	"DCVDD",
261 	"AVDD1",
262 	"AVDD2",
263 	"CPVDD",
264 	"SPKVDD1",
265 	"SPKVDD2",
266 };
267 
268 #ifdef CONFIG_PM
269 static int wm8994_suspend(struct device *dev)
270 {
271 	struct wm8994 *wm8994 = dev_get_drvdata(dev);
272 	int ret;
273 
274 	/* Don't actually go through with the suspend if the CODEC is
275 	 * still active (eg, for audio passthrough from CP. */
276 	ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_1);
277 	if (ret < 0) {
278 		dev_err(dev, "Failed to read power status: %d\n", ret);
279 	} else if (ret & WM8994_VMID_SEL_MASK) {
280 		dev_dbg(dev, "CODEC still active, ignoring suspend\n");
281 		return 0;
282 	}
283 
284 	/* GPIO configuration state is saved here since we may be configuring
285 	 * the GPIO alternate functions even if we're not using the gpiolib
286 	 * driver for them.
287 	 */
288 	ret = wm8994_read(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2,
289 			  &wm8994->gpio_regs);
290 	if (ret < 0)
291 		dev_err(dev, "Failed to save GPIO registers: %d\n", ret);
292 
293 	/* For similar reasons we also stash the regulator states */
294 	ret = wm8994_read(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2,
295 			  &wm8994->ldo_regs);
296 	if (ret < 0)
297 		dev_err(dev, "Failed to save LDO registers: %d\n", ret);
298 
299 	/* Explicitly put the device into reset in case regulators
300 	 * don't get disabled in order to ensure consistent restart.
301 	 */
302 	wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET, 0x8994);
303 
304 	wm8994->suspended = true;
305 
306 	ret = regulator_bulk_disable(wm8994->num_supplies,
307 				     wm8994->supplies);
308 	if (ret != 0) {
309 		dev_err(dev, "Failed to disable supplies: %d\n", ret);
310 		return ret;
311 	}
312 
313 	return 0;
314 }
315 
316 static int wm8994_resume(struct device *dev)
317 {
318 	struct wm8994 *wm8994 = dev_get_drvdata(dev);
319 	int ret, i;
320 
321 	/* We may have lied to the PM core about suspending */
322 	if (!wm8994->suspended)
323 		return 0;
324 
325 	ret = regulator_bulk_enable(wm8994->num_supplies,
326 				    wm8994->supplies);
327 	if (ret != 0) {
328 		dev_err(dev, "Failed to enable supplies: %d\n", ret);
329 		return ret;
330 	}
331 
332 	/* Write register at a time as we use the cache on the CPU so store
333 	 * it in native endian.
334 	 */
335 	for (i = 0; i < ARRAY_SIZE(wm8994->irq_masks_cur); i++) {
336 		ret = wm8994_reg_write(wm8994, WM8994_INTERRUPT_STATUS_1_MASK
337 				       + i, wm8994->irq_masks_cur[i]);
338 		if (ret < 0)
339 			dev_err(dev, "Failed to restore interrupt masks: %d\n",
340 				ret);
341 	}
342 
343 	ret = wm8994_write(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2,
344 			   &wm8994->ldo_regs);
345 	if (ret < 0)
346 		dev_err(dev, "Failed to restore LDO registers: %d\n", ret);
347 
348 	ret = wm8994_write(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2,
349 			   &wm8994->gpio_regs);
350 	if (ret < 0)
351 		dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
352 
353 	wm8994->suspended = false;
354 
355 	return 0;
356 }
357 #endif
358 
359 #ifdef CONFIG_REGULATOR
360 static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
361 {
362 	struct wm8994_ldo_pdata *ldo_pdata;
363 
364 	if (!pdata)
365 		return 0;
366 
367 	ldo_pdata = &pdata->ldo[ldo];
368 
369 	if (!ldo_pdata->init_data)
370 		return 0;
371 
372 	return ldo_pdata->init_data->num_consumer_supplies != 0;
373 }
374 #else
375 static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
376 {
377 	return 0;
378 }
379 #endif
380 
381 /*
382  * Instantiate the generic non-control parts of the device.
383  */
384 static int wm8994_device_init(struct wm8994 *wm8994, int irq)
385 {
386 	struct wm8994_pdata *pdata = wm8994->dev->platform_data;
387 	const char *devname;
388 	int ret, i;
389 
390 	mutex_init(&wm8994->io_lock);
391 	dev_set_drvdata(wm8994->dev, wm8994);
392 
393 	/* Add the on-chip regulators first for bootstrapping */
394 	ret = mfd_add_devices(wm8994->dev, -1,
395 			      wm8994_regulator_devs,
396 			      ARRAY_SIZE(wm8994_regulator_devs),
397 			      NULL, 0);
398 	if (ret != 0) {
399 		dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
400 		goto err;
401 	}
402 
403 	switch (wm8994->type) {
404 	case WM8994:
405 		wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies);
406 		break;
407 	case WM8958:
408 		wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies);
409 		break;
410 	default:
411 		BUG();
412 		goto err;
413 	}
414 
415 	wm8994->supplies = kzalloc(sizeof(struct regulator_bulk_data) *
416 				   wm8994->num_supplies,
417 				   GFP_KERNEL);
418 	if (!wm8994->supplies) {
419 		ret = -ENOMEM;
420 		goto err;
421 	}
422 
423 	switch (wm8994->type) {
424 	case WM8994:
425 		for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
426 			wm8994->supplies[i].supply = wm8994_main_supplies[i];
427 		break;
428 	case WM8958:
429 		for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++)
430 			wm8994->supplies[i].supply = wm8958_main_supplies[i];
431 		break;
432 	default:
433 		BUG();
434 		goto err;
435 	}
436 
437 	ret = regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
438 				 wm8994->supplies);
439 	if (ret != 0) {
440 		dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
441 		goto err_supplies;
442 	}
443 
444 	ret = regulator_bulk_enable(wm8994->num_supplies,
445 				    wm8994->supplies);
446 	if (ret != 0) {
447 		dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
448 		goto err_get;
449 	}
450 
451 	ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
452 	if (ret < 0) {
453 		dev_err(wm8994->dev, "Failed to read ID register\n");
454 		goto err_enable;
455 	}
456 	switch (ret) {
457 	case 0x8994:
458 		devname = "WM8994";
459 		if (wm8994->type != WM8994)
460 			dev_warn(wm8994->dev, "Device registered as type %d\n",
461 				 wm8994->type);
462 		wm8994->type = WM8994;
463 		break;
464 	case 0x8958:
465 		devname = "WM8958";
466 		if (wm8994->type != WM8958)
467 			dev_warn(wm8994->dev, "Device registered as type %d\n",
468 				 wm8994->type);
469 		wm8994->type = WM8958;
470 		break;
471 	default:
472 		dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
473 			ret);
474 		ret = -EINVAL;
475 		goto err_enable;
476 	}
477 
478 	ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
479 	if (ret < 0) {
480 		dev_err(wm8994->dev, "Failed to read revision register: %d\n",
481 			ret);
482 		goto err_enable;
483 	}
484 
485 	switch (wm8994->type) {
486 	case WM8994:
487 		switch (ret) {
488 		case 0:
489 		case 1:
490 			dev_warn(wm8994->dev,
491 				 "revision %c not fully supported\n",
492 				 'A' + ret);
493 			break;
494 		default:
495 			break;
496 		}
497 		break;
498 	default:
499 		break;
500 	}
501 
502 	dev_info(wm8994->dev, "%s revision %c\n", devname, 'A' + ret);
503 
504 	if (pdata) {
505 		wm8994->irq_base = pdata->irq_base;
506 		wm8994->gpio_base = pdata->gpio_base;
507 
508 		/* GPIO configuration is only applied if it's non-zero */
509 		for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
510 			if (pdata->gpio_defaults[i]) {
511 				wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
512 						0xffff,
513 						pdata->gpio_defaults[i]);
514 			}
515 		}
516 	}
517 
518 	/* In some system designs where the regulators are not in use,
519 	 * we can achieve a small reduction in leakage currents by
520 	 * floating LDO outputs.  This bit makes no difference if the
521 	 * LDOs are enabled, it only affects cases where the LDOs were
522 	 * in operation and are then disabled.
523 	 */
524 	for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
525 		if (wm8994_ldo_in_use(pdata, i))
526 			wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
527 					WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
528 		else
529 			wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
530 					WM8994_LDO1_DISCH, 0);
531 	}
532 
533 	wm8994_irq_init(wm8994);
534 
535 	ret = mfd_add_devices(wm8994->dev, -1,
536 			      wm8994_devs, ARRAY_SIZE(wm8994_devs),
537 			      NULL, 0);
538 	if (ret != 0) {
539 		dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
540 		goto err_irq;
541 	}
542 
543 	pm_runtime_enable(wm8994->dev);
544 	pm_runtime_resume(wm8994->dev);
545 
546 	return 0;
547 
548 err_irq:
549 	wm8994_irq_exit(wm8994);
550 err_enable:
551 	regulator_bulk_disable(wm8994->num_supplies,
552 			       wm8994->supplies);
553 err_get:
554 	regulator_bulk_free(wm8994->num_supplies, wm8994->supplies);
555 err_supplies:
556 	kfree(wm8994->supplies);
557 err:
558 	mfd_remove_devices(wm8994->dev);
559 	kfree(wm8994);
560 	return ret;
561 }
562 
563 static void wm8994_device_exit(struct wm8994 *wm8994)
564 {
565 	pm_runtime_disable(wm8994->dev);
566 	mfd_remove_devices(wm8994->dev);
567 	wm8994_irq_exit(wm8994);
568 	regulator_bulk_disable(wm8994->num_supplies,
569 			       wm8994->supplies);
570 	regulator_bulk_free(wm8994->num_supplies, wm8994->supplies);
571 	kfree(wm8994->supplies);
572 	kfree(wm8994);
573 }
574 
575 static int wm8994_i2c_read_device(struct wm8994 *wm8994, unsigned short reg,
576 				  int bytes, void *dest)
577 {
578 	struct i2c_client *i2c = wm8994->control_data;
579 	int ret;
580 	u16 r = cpu_to_be16(reg);
581 
582 	ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
583 	if (ret < 0)
584 		return ret;
585 	if (ret != 2)
586 		return -EIO;
587 
588 	ret = i2c_master_recv(i2c, dest, bytes);
589 	if (ret < 0)
590 		return ret;
591 	if (ret != bytes)
592 		return -EIO;
593 	return 0;
594 }
595 
596 static int wm8994_i2c_write_device(struct wm8994 *wm8994, unsigned short reg,
597 				   int bytes, const void *src)
598 {
599 	struct i2c_client *i2c = wm8994->control_data;
600 	struct i2c_msg xfer[2];
601 	int ret;
602 
603 	reg = cpu_to_be16(reg);
604 
605 	xfer[0].addr = i2c->addr;
606 	xfer[0].flags = 0;
607 	xfer[0].len = 2;
608 	xfer[0].buf = (char *)&reg;
609 
610 	xfer[1].addr = i2c->addr;
611 	xfer[1].flags = I2C_M_NOSTART;
612 	xfer[1].len = bytes;
613 	xfer[1].buf = (char *)src;
614 
615 	ret = i2c_transfer(i2c->adapter, xfer, 2);
616 	if (ret < 0)
617 		return ret;
618 	if (ret != 2)
619 		return -EIO;
620 
621 	return 0;
622 }
623 
624 static int wm8994_i2c_probe(struct i2c_client *i2c,
625 			    const struct i2c_device_id *id)
626 {
627 	struct wm8994 *wm8994;
628 
629 	wm8994 = kzalloc(sizeof(struct wm8994), GFP_KERNEL);
630 	if (wm8994 == NULL)
631 		return -ENOMEM;
632 
633 	i2c_set_clientdata(i2c, wm8994);
634 	wm8994->dev = &i2c->dev;
635 	wm8994->control_data = i2c;
636 	wm8994->read_dev = wm8994_i2c_read_device;
637 	wm8994->write_dev = wm8994_i2c_write_device;
638 	wm8994->irq = i2c->irq;
639 	wm8994->type = id->driver_data;
640 
641 	return wm8994_device_init(wm8994, i2c->irq);
642 }
643 
644 static int wm8994_i2c_remove(struct i2c_client *i2c)
645 {
646 	struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
647 
648 	wm8994_device_exit(wm8994);
649 
650 	return 0;
651 }
652 
653 static const struct i2c_device_id wm8994_i2c_id[] = {
654 	{ "wm8994", WM8994 },
655 	{ "wm8958", WM8958 },
656 	{ }
657 };
658 MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
659 
660 static UNIVERSAL_DEV_PM_OPS(wm8994_pm_ops, wm8994_suspend, wm8994_resume,
661 			    NULL);
662 
663 static struct i2c_driver wm8994_i2c_driver = {
664 	.driver = {
665 		.name = "wm8994",
666 		.owner = THIS_MODULE,
667 		.pm = &wm8994_pm_ops,
668 	},
669 	.probe = wm8994_i2c_probe,
670 	.remove = wm8994_i2c_remove,
671 	.id_table = wm8994_i2c_id,
672 };
673 
674 static int __init wm8994_i2c_init(void)
675 {
676 	int ret;
677 
678 	ret = i2c_add_driver(&wm8994_i2c_driver);
679 	if (ret != 0)
680 		pr_err("Failed to register wm8994 I2C driver: %d\n", ret);
681 
682 	return ret;
683 }
684 module_init(wm8994_i2c_init);
685 
686 static void __exit wm8994_i2c_exit(void)
687 {
688 	i2c_del_driver(&wm8994_i2c_driver);
689 }
690 module_exit(wm8994_i2c_exit);
691 
692 MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
693 MODULE_LICENSE("GPL");
694 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
695