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