Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * MFD driver for wl1273 FM radio and audio codec submodules.
0004  *
0005  * Copyright (C) 2011 Nokia Corporation
0006  * Author: Matti Aaltonen <matti.j.aaltonen@nokia.com>
0007  */
0008 
0009 #include <linux/mfd/wl1273-core.h>
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 
0013 #define DRIVER_DESC "WL1273 FM Radio Core"
0014 
0015 static const struct i2c_device_id wl1273_driver_id_table[] = {
0016     { WL1273_FM_DRIVER_NAME, 0 },
0017     { }
0018 };
0019 MODULE_DEVICE_TABLE(i2c, wl1273_driver_id_table);
0020 
0021 static int wl1273_fm_read_reg(struct wl1273_core *core, u8 reg, u16 *value)
0022 {
0023     struct i2c_client *client = core->client;
0024     u8 b[2];
0025     int r;
0026 
0027     r = i2c_smbus_read_i2c_block_data(client, reg, sizeof(b), b);
0028     if (r != 2) {
0029         dev_err(&client->dev, "%s: Read: %d fails.\n", __func__, reg);
0030         return -EREMOTEIO;
0031     }
0032 
0033     *value = (u16)b[0] << 8 | b[1];
0034 
0035     return 0;
0036 }
0037 
0038 static int wl1273_fm_write_cmd(struct wl1273_core *core, u8 cmd, u16 param)
0039 {
0040     struct i2c_client *client = core->client;
0041     u8 buf[] = { (param >> 8) & 0xff, param & 0xff };
0042     int r;
0043 
0044     r = i2c_smbus_write_i2c_block_data(client, cmd, sizeof(buf), buf);
0045     if (r) {
0046         dev_err(&client->dev, "%s: Cmd: %d fails.\n", __func__, cmd);
0047         return r;
0048     }
0049 
0050     return 0;
0051 }
0052 
0053 static int wl1273_fm_write_data(struct wl1273_core *core, u8 *data, u16 len)
0054 {
0055     struct i2c_client *client = core->client;
0056     struct i2c_msg msg;
0057     int r;
0058 
0059     msg.addr = client->addr;
0060     msg.flags = 0;
0061     msg.buf = data;
0062     msg.len = len;
0063 
0064     r = i2c_transfer(client->adapter, &msg, 1);
0065     if (r != 1) {
0066         dev_err(&client->dev, "%s: write error.\n", __func__);
0067         return -EREMOTEIO;
0068     }
0069 
0070     return 0;
0071 }
0072 
0073 /**
0074  * wl1273_fm_set_audio() -  Set audio mode.
0075  * @core:           A pointer to the device struct.
0076  * @new_mode:           The new audio mode.
0077  *
0078  * Audio modes are WL1273_AUDIO_DIGITAL and WL1273_AUDIO_ANALOG.
0079  */
0080 static int wl1273_fm_set_audio(struct wl1273_core *core, unsigned int new_mode)
0081 {
0082     int r = 0;
0083 
0084     if (core->mode == WL1273_MODE_OFF ||
0085         core->mode == WL1273_MODE_SUSPENDED)
0086         return -EPERM;
0087 
0088     if (core->mode == WL1273_MODE_RX && new_mode == WL1273_AUDIO_DIGITAL) {
0089         r = wl1273_fm_write_cmd(core, WL1273_PCM_MODE_SET,
0090                     WL1273_PCM_DEF_MODE);
0091         if (r)
0092             goto out;
0093 
0094         r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
0095                     core->i2s_mode);
0096         if (r)
0097             goto out;
0098 
0099         r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
0100                     WL1273_AUDIO_ENABLE_I2S);
0101         if (r)
0102             goto out;
0103 
0104     } else if (core->mode == WL1273_MODE_RX &&
0105            new_mode == WL1273_AUDIO_ANALOG) {
0106         r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
0107                     WL1273_AUDIO_ENABLE_ANALOG);
0108         if (r)
0109             goto out;
0110 
0111     } else if (core->mode == WL1273_MODE_TX &&
0112            new_mode == WL1273_AUDIO_DIGITAL) {
0113         r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET,
0114                     core->i2s_mode);
0115         if (r)
0116             goto out;
0117 
0118         r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
0119                     WL1273_AUDIO_IO_SET_I2S);
0120         if (r)
0121             goto out;
0122 
0123     } else if (core->mode == WL1273_MODE_TX &&
0124            new_mode == WL1273_AUDIO_ANALOG) {
0125         r = wl1273_fm_write_cmd(core, WL1273_AUDIO_IO_SET,
0126                     WL1273_AUDIO_IO_SET_ANALOG);
0127         if (r)
0128             goto out;
0129     }
0130 
0131     core->audio_mode = new_mode;
0132 out:
0133     return r;
0134 }
0135 
0136 /**
0137  * wl1273_fm_set_volume() - Set volume.
0138  * @core:           A pointer to the device struct.
0139  * @volume:         The new volume value.
0140  */
0141 static int wl1273_fm_set_volume(struct wl1273_core *core, unsigned int volume)
0142 {
0143     int r;
0144 
0145     if (volume > WL1273_MAX_VOLUME)
0146         return -EINVAL;
0147 
0148     if (core->volume == volume)
0149         return 0;
0150 
0151     r = wl1273_fm_write_cmd(core, WL1273_VOLUME_SET, volume);
0152     if (r)
0153         return r;
0154 
0155     core->volume = volume;
0156     return 0;
0157 }
0158 
0159 static int wl1273_core_probe(struct i2c_client *client,
0160                        const struct i2c_device_id *id)
0161 {
0162     struct wl1273_fm_platform_data *pdata = dev_get_platdata(&client->dev);
0163     struct wl1273_core *core;
0164     struct mfd_cell *cell;
0165     int children = 0;
0166     int r = 0;
0167 
0168     dev_dbg(&client->dev, "%s\n", __func__);
0169 
0170     if (!pdata) {
0171         dev_err(&client->dev, "No platform data.\n");
0172         return -EINVAL;
0173     }
0174 
0175     if (!(pdata->children & WL1273_RADIO_CHILD)) {
0176         dev_err(&client->dev, "Cannot function without radio child.\n");
0177         return -EINVAL;
0178     }
0179 
0180     core = devm_kzalloc(&client->dev, sizeof(*core), GFP_KERNEL);
0181     if (!core)
0182         return -ENOMEM;
0183 
0184     core->pdata = pdata;
0185     core->client = client;
0186     mutex_init(&core->lock);
0187 
0188     i2c_set_clientdata(client, core);
0189 
0190     dev_dbg(&client->dev, "%s: Have V4L2.\n", __func__);
0191 
0192     cell = &core->cells[children];
0193     cell->name = "wl1273_fm_radio";
0194     cell->platform_data = &core;
0195     cell->pdata_size = sizeof(core);
0196     children++;
0197 
0198     core->read = wl1273_fm_read_reg;
0199     core->write = wl1273_fm_write_cmd;
0200     core->write_data = wl1273_fm_write_data;
0201     core->set_audio = wl1273_fm_set_audio;
0202     core->set_volume = wl1273_fm_set_volume;
0203 
0204     if (pdata->children & WL1273_CODEC_CHILD) {
0205         cell = &core->cells[children];
0206 
0207         dev_dbg(&client->dev, "%s: Have codec.\n", __func__);
0208         cell->name = "wl1273-codec";
0209         cell->platform_data = &core;
0210         cell->pdata_size = sizeof(core);
0211         children++;
0212     }
0213 
0214     dev_dbg(&client->dev, "%s: number of children: %d.\n",
0215         __func__, children);
0216 
0217     r = devm_mfd_add_devices(&client->dev, -1, core->cells,
0218                  children, NULL, 0, NULL);
0219     if (r)
0220         goto err;
0221 
0222     return 0;
0223 
0224 err:
0225     pdata->free_resources();
0226 
0227     dev_dbg(&client->dev, "%s\n", __func__);
0228 
0229     return r;
0230 }
0231 
0232 static struct i2c_driver wl1273_core_driver = {
0233     .driver = {
0234         .name = WL1273_FM_DRIVER_NAME,
0235     },
0236     .probe = wl1273_core_probe,
0237     .id_table = wl1273_driver_id_table,
0238 };
0239 
0240 static int __init wl1273_core_init(void)
0241 {
0242     int r;
0243 
0244     r = i2c_add_driver(&wl1273_core_driver);
0245     if (r) {
0246         pr_err(WL1273_FM_DRIVER_NAME
0247                ": driver registration failed\n");
0248         return r;
0249     }
0250 
0251     return r;
0252 }
0253 
0254 static void __exit wl1273_core_exit(void)
0255 {
0256     i2c_del_driver(&wl1273_core_driver);
0257 }
0258 late_initcall(wl1273_core_init);
0259 module_exit(wl1273_core_exit);
0260 
0261 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
0262 MODULE_DESCRIPTION(DRIVER_DESC);
0263 MODULE_LICENSE("GPL");