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