xref: /openbmc/u-boot/drivers/sound/maxim_codec.c (revision dd1033e4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * maxim_codec.c -- MAXIM CODEC Common driver
4  *
5  * Copyright 2011 Maxim Integrated Products
6  */
7 
8 #include <common.h>
9 #include <div64.h>
10 #include <i2c.h>
11 #include <i2s.h>
12 #include <sound.h>
13 #include <asm/gpio.h>
14 #include <asm/io.h>
15 #include <asm/arch/clk.h>
16 #include <asm/arch/cpu.h>
17 #include <asm/arch/power.h>
18 #include "maxim_codec.h"
19 
20 /*
21  * Writes value to a device register through i2c
22  *
23  * @param priv	Private data for driver
24  * @param reg	reg number to be write
25  * @param data	data to be writen to the above registor
26  *
27  * @return	int value 1 for change, 0 for no change or negative error code.
28  */
29 int maxim_i2c_write(struct maxim_priv *priv, unsigned int reg,
30 		    unsigned char data)
31 {
32 	debug("%s: Write Addr : 0x%02X, Data :  0x%02X\n",
33 	      __func__, reg, data);
34 	return dm_i2c_write(priv->dev, reg, &data, 1);
35 }
36 
37 /*
38  * Read a value from a device register through i2c
39  *
40  * @param priv	Private data for driver
41  * @param reg	reg number to be read
42  * @param data	address of read data to be stored
43  *
44  * @return	int value 0 for success, -1 in case of error.
45  */
46 unsigned int maxim_i2c_read(struct maxim_priv *priv, unsigned int reg,
47 			    unsigned char *data)
48 {
49 	int ret;
50 
51 	return dm_i2c_read(priv->dev, reg, data, 1);
52 	if (ret != 0) {
53 		debug("%s: Error while reading register %#04x\n",
54 		      __func__, reg);
55 		return -1;
56 	}
57 
58 	return 0;
59 }
60 
61 /*
62  * update device register bits through i2c
63  *
64  * @param priv	Private data for driver
65  * @param reg	codec register
66  * @param mask	register mask
67  * @param value	new value
68  *
69  * @return int value 0 for success, non-zero error code.
70  */
71 int maxim_bic_or(struct maxim_priv *priv, unsigned int reg, unsigned char mask,
72 		 unsigned char value)
73 {
74 	int change, ret = 0;
75 	unsigned char old, new;
76 
77 	if (maxim_i2c_read(priv, reg, &old) != 0)
78 		return -1;
79 	new = (old & ~mask) | (value & mask);
80 	change  = (old != new) ? 1 : 0;
81 	if (change)
82 		ret = maxim_i2c_write(priv, reg, new);
83 	if (ret < 0)
84 		return ret;
85 
86 	return change;
87 }
88