blob: 9554a0c506afbc60239453e8cfd5ea719e6ac695 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * common keywest i2c layer
4 *
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/i2c.h>
11#include <linux/delay.h>
Takashi Iwai654e2752015-08-25 14:04:44 +020012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <sound/core.h>
14#include "pmac.h"
15
16/*
17 * we have to keep a static variable here since i2c attach_adapter
18 * callback cannot pass a private data.
19 */
Takashi Iwai65b29f52005-11-17 15:09:46 +010020static struct pmac_keywest *keywest_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Wolfram Sang28760c12015-05-23 18:15:11 +020022static bool keywest_probed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jean Delvare5de41552009-04-20 22:56:59 +020024static int keywest_probe(struct i2c_client *client,
25 const struct i2c_device_id *id)
26{
Wolfram Sang28760c12015-05-23 18:15:11 +020027 keywest_probed = true;
28 /* If instantiated via i2c-powermac, we still need to set the client */
29 if (!keywest_ctx->client)
30 keywest_ctx->client = client;
Jean Delvare5de41552009-04-20 22:56:59 +020031 i2c_set_clientdata(client, keywest_ctx);
32 return 0;
33}
34
35/*
36 * This is kind of a hack, best would be to turn powermac to fixed i2c
37 * bus numbers and declare the sound device as part of platform
38 * initialization
39 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int keywest_attach_adapter(struct i2c_adapter *adapter)
41{
Jean Delvare5de41552009-04-20 22:56:59 +020042 struct i2c_board_info info;
Wolfram Sang04a9af22020-03-26 22:10:12 +010043 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (! keywest_ctx)
46 return -EINVAL;
47
Jean Delvare903dba12009-05-14 14:37:21 +020048 if (strncmp(adapter->name, "mac-io", 6))
Wolfram Sangac397c82015-05-09 19:42:22 +020049 return -EINVAL; /* ignored */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jean Delvare5de41552009-04-20 22:56:59 +020051 memset(&info, 0, sizeof(struct i2c_board_info));
52 strlcpy(info.type, "keywest", I2C_NAME_SIZE);
53 info.addr = keywest_ctx->addr;
Wolfram Sang04a9af22020-03-26 22:10:12 +010054 client = i2c_new_client_device(adapter, &info);
55 if (IS_ERR(client))
56 return PTR_ERR(client);
57 keywest_ctx->client = client;
58
Takashi Iwai18c40782009-10-01 07:46:33 +020059 /*
60 * We know the driver is already loaded, so the device should be
61 * already bound. If not it means binding failed, and then there
62 * is no point in keeping the device instantiated.
63 */
Lars-Peter Clausena7cde6d2013-09-29 10:51:04 +020064 if (!keywest_ctx->client->dev.driver) {
Takashi Iwai18c40782009-10-01 07:46:33 +020065 i2c_unregister_device(keywest_ctx->client);
66 keywest_ctx->client = NULL;
67 return -ENODEV;
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jean Delvare5de41552009-04-20 22:56:59 +020070 /*
71 * Let i2c-core delete that device on driver removal.
72 * This is safe because i2c-core holds the core_lock mutex for us.
73 */
74 list_add_tail(&keywest_ctx->client->detected,
Lars-Peter Clausena7cde6d2013-09-29 10:51:04 +020075 &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Jean Delvare5de41552009-04-20 22:56:59 +020079static int keywest_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 if (! keywest_ctx)
82 return 0;
83 if (client == keywest_ctx->client)
84 keywest_ctx->client = NULL;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87}
88
Jean Delvare5de41552009-04-20 22:56:59 +020089
90static const struct i2c_device_id keywest_i2c_id[] = {
Wolfram Sang28760c12015-05-23 18:15:11 +020091 { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */
92 { "keywest", 0 }, /* instantiated by us if needed */
Jean Delvare5de41552009-04-20 22:56:59 +020093 { }
94};
Javier Martinez Canillasa2bc2af2015-08-25 08:31:14 +020095MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
Jean Delvare5de41552009-04-20 22:56:59 +020096
Jean Delvarea656cbf2009-10-01 18:08:18 +020097static struct i2c_driver keywest_driver = {
Jean Delvare5de41552009-04-20 22:56:59 +020098 .driver = {
99 .name = "PMac Keywest Audio",
100 },
Jean Delvare5de41552009-04-20 22:56:59 +0200101 .probe = keywest_probe,
102 .remove = keywest_remove,
103 .id_table = keywest_i2c_id,
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* exported */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100107void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 if (keywest_ctx && keywest_ctx == i2c) {
110 i2c_del_driver(&keywest_driver);
111 keywest_ctx = NULL;
112 }
113}
114
Bill Pemberton15afafc2012-12-06 12:35:23 -0500115int snd_pmac_tumbler_post_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 int err;
118
Takashi Iwai783eaf42006-09-17 22:00:51 +0200119 if (!keywest_ctx || !keywest_ctx->client)
120 return -ENXIO;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if ((err = keywest_ctx->init_client(keywest_ctx)) < 0) {
123 snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
124 return err;
125 }
126 return 0;
127}
128
129/* exported */
Bill Pemberton15afafc2012-12-06 12:35:23 -0500130int snd_pmac_keywest_init(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Wolfram Sangac397c82015-05-09 19:42:22 +0200132 struct i2c_adapter *adap;
133 int err, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (keywest_ctx)
136 return -EBUSY;
137
Wolfram Sangac397c82015-05-09 19:42:22 +0200138 adap = i2c_get_adapter(0);
139 if (!adap)
140 return -EPROBE_DEFER;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 keywest_ctx = i2c;
143
144 if ((err = i2c_add_driver(&keywest_driver))) {
145 snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
Wolfram Sangac397c82015-05-09 19:42:22 +0200146 i2c_put_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return err;
148 }
Wolfram Sangac397c82015-05-09 19:42:22 +0200149
Wolfram Sang28760c12015-05-23 18:15:11 +0200150 /* There was already a device from i2c-powermac. Great, let's return */
151 if (keywest_probed)
152 return 0;
153
Wolfram Sangac397c82015-05-09 19:42:22 +0200154 /* We assume Macs have consecutive I2C bus numbers starting at 0 */
155 while (adap) {
Wolfram Sang28760c12015-05-23 18:15:11 +0200156 /* Scan for devices to be bound to */
Wolfram Sangac397c82015-05-09 19:42:22 +0200157 err = keywest_attach_adapter(adap);
158 if (!err)
159 return 0;
160 i2c_put_adapter(adap);
161 adap = i2c_get_adapter(++i);
162 }
163
164 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}