Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <console/console.h> |
Tim Crawford | ca11545 | 2024-03-07 15:21:59 -0700 | [diff] [blame^] | 4 | #include <device/i2c_bus.h> |
Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 5 | #include "chip.h" |
| 6 | #include "tas5825m.h" |
| 7 | |
| 8 | int tas5825m_write_at(struct device *dev, uint8_t addr, uint8_t value) |
| 9 | { |
Tim Crawford | ca11545 | 2024-03-07 15:21:59 -0700 | [diff] [blame^] | 10 | return i2c_dev_writeb_at(dev, addr, value); |
Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 11 | } |
| 12 | |
Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 13 | int tas5825m_write_block_at(struct device *dev, uint8_t addr, |
| 14 | const uint8_t *values, uint8_t length) |
| 15 | { |
Tim Crawford | ca11545 | 2024-03-07 15:21:59 -0700 | [diff] [blame^] | 16 | // TODO: use I2C block write for better performance; SMBus does not |
| 17 | // have `transfer` op for it. |
| 18 | |
Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 19 | int res = 0; |
| 20 | for (uint8_t i = 0; i < length; i++) { |
Tim Crawford | ca11545 | 2024-03-07 15:21:59 -0700 | [diff] [blame^] | 21 | res = i2c_dev_writeb_at(dev, addr + i, values[i]); |
Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 22 | if (res < 0) |
| 23 | return res; |
| 24 | } |
| 25 | return (int)length; |
| 26 | } |
| 27 | |
| 28 | int tas5825m_set_page(struct device *dev, uint8_t page) |
| 29 | { |
| 30 | return tas5825m_write_at(dev, 0x00, page); |
| 31 | } |
| 32 | |
| 33 | int tas5825m_set_book(struct device *dev, uint8_t book) |
| 34 | { |
| 35 | int res = tas5825m_set_page(dev, 0x00); |
| 36 | if (res < 0) |
| 37 | return res; |
| 38 | return tas5825m_write_at(dev, 0x7F, book); |
| 39 | } |
| 40 | |
| 41 | __weak int tas5825m_setup(struct device *dev, int id) |
| 42 | { |
| 43 | printk(BIOS_ERR, "tas5825m: setup not implemented\n"); |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | static void tas5825m_init(struct device *dev) |
| 48 | { |
Tim Crawford | ca11545 | 2024-03-07 15:21:59 -0700 | [diff] [blame^] | 49 | if (dev->enabled && dev->path.type == DEVICE_PATH_I2C && i2c_link(dev)) { |
Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 50 | printk(BIOS_DEBUG, "tas5825m at %s\n", dev_path(dev)); |
| 51 | |
| 52 | struct drivers_i2c_tas5825m_config *config = dev->chip_info; |
| 53 | if (config) { |
| 54 | printk(BIOS_DEBUG, "tas5825m id %d\n", config->id); |
| 55 | int res = tas5825m_setup(dev, config->id); |
| 56 | if (res) |
| 57 | printk(BIOS_ERR, "tas5825m init failed: %d\n", res); |
| 58 | else |
| 59 | printk(BIOS_DEBUG, "tas5825m init successful\n"); |
| 60 | } else { |
| 61 | printk(BIOS_ERR, "tas5825m: failed to find config\n"); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static struct device_operations tas5825m_operations = { |
| 67 | .read_resources = noop_read_resources, |
| 68 | .set_resources = noop_set_resources, |
| 69 | .init = tas5825m_init, |
| 70 | }; |
| 71 | |
| 72 | static void tas5825m_enable_dev(struct device *dev) |
| 73 | { |
| 74 | dev->ops = &tas5825m_operations; |
| 75 | } |
| 76 | |
| 77 | struct chip_operations drivers_i2c_tas5825m_ops = { |
Nicholas Sudsgaard | bfb11be | 2024-01-30 09:53:46 +0900 | [diff] [blame] | 78 | .name = "TI TAS5825M Amplifier", |
Jeremy Soller | e20c47b | 2021-01-26 13:21:12 -0700 | [diff] [blame] | 79 | .enable_dev = tas5825m_enable_dev, |
| 80 | }; |