blob: 6e5daae18f9d0edb813016292b2a1809d9e3aa9b [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
Takashi Iwai65b29f52005-11-17 15:09:46 +010016static struct pmac_keywest *keywest_ctx;
Wolfram Sang28760c12015-05-23 18:15:11 +020017static bool keywest_probed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jean Delvare5de41552009-04-20 22:56:59 +020019static int keywest_probe(struct i2c_client *client,
20 const struct i2c_device_id *id)
21{
Wolfram Sang28760c12015-05-23 18:15:11 +020022 keywest_probed = true;
23 /* If instantiated via i2c-powermac, we still need to set the client */
24 if (!keywest_ctx->client)
25 keywest_ctx->client = client;
Jean Delvare5de41552009-04-20 22:56:59 +020026 i2c_set_clientdata(client, keywest_ctx);
27 return 0;
28}
29
30/*
31 * This is kind of a hack, best would be to turn powermac to fixed i2c
32 * bus numbers and declare the sound device as part of platform
33 * initialization
34 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int keywest_attach_adapter(struct i2c_adapter *adapter)
36{
Jean Delvare5de41552009-04-20 22:56:59 +020037 struct i2c_board_info info;
Wolfram Sang04a9af22020-03-26 22:10:12 +010038 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 if (! keywest_ctx)
41 return -EINVAL;
42
Jean Delvare903dba12009-05-14 14:37:21 +020043 if (strncmp(adapter->name, "mac-io", 6))
Wolfram Sangac397c82015-05-09 19:42:22 +020044 return -EINVAL; /* ignored */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jean Delvare5de41552009-04-20 22:56:59 +020046 memset(&info, 0, sizeof(struct i2c_board_info));
Joe Perches75b1a8f2021-01-04 09:17:34 -080047 strscpy(info.type, "keywest", I2C_NAME_SIZE);
Jean Delvare5de41552009-04-20 22:56:59 +020048 info.addr = keywest_ctx->addr;
Wolfram Sang04a9af22020-03-26 22:10:12 +010049 client = i2c_new_client_device(adapter, &info);
50 if (IS_ERR(client))
51 return PTR_ERR(client);
52 keywest_ctx->client = client;
53
Takashi Iwai18c40782009-10-01 07:46:33 +020054 /*
55 * We know the driver is already loaded, so the device should be
56 * already bound. If not it means binding failed, and then there
57 * is no point in keeping the device instantiated.
58 */
Lars-Peter Clausena7cde6d2013-09-29 10:51:04 +020059 if (!keywest_ctx->client->dev.driver) {
Takashi Iwai18c40782009-10-01 07:46:33 +020060 i2c_unregister_device(keywest_ctx->client);
61 keywest_ctx->client = NULL;
62 return -ENODEV;
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jean Delvare5de41552009-04-20 22:56:59 +020065 /*
66 * Let i2c-core delete that device on driver removal.
67 * This is safe because i2c-core holds the core_lock mutex for us.
68 */
69 list_add_tail(&keywest_ctx->client->detected,
Lars-Peter Clausena7cde6d2013-09-29 10:51:04 +020070 &to_i2c_driver(keywest_ctx->client->dev.driver)->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Jean Delvare5de41552009-04-20 22:56:59 +020074static int keywest_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 if (! keywest_ctx)
77 return 0;
78 if (client == keywest_ctx->client)
79 keywest_ctx->client = NULL;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82}
83
Jean Delvare5de41552009-04-20 22:56:59 +020084
85static const struct i2c_device_id keywest_i2c_id[] = {
Wolfram Sang28760c12015-05-23 18:15:11 +020086 { "MAC,tas3004", 0 }, /* instantiated by i2c-powermac */
87 { "keywest", 0 }, /* instantiated by us if needed */
Jean Delvare5de41552009-04-20 22:56:59 +020088 { }
89};
Javier Martinez Canillasa2bc2af2015-08-25 08:31:14 +020090MODULE_DEVICE_TABLE(i2c, keywest_i2c_id);
Jean Delvare5de41552009-04-20 22:56:59 +020091
Jean Delvarea656cbf2009-10-01 18:08:18 +020092static struct i2c_driver keywest_driver = {
Jean Delvare5de41552009-04-20 22:56:59 +020093 .driver = {
94 .name = "PMac Keywest Audio",
95 },
Jean Delvare5de41552009-04-20 22:56:59 +020096 .probe = keywest_probe,
97 .remove = keywest_remove,
98 .id_table = keywest_i2c_id,
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* exported */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100102void snd_pmac_keywest_cleanup(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 if (keywest_ctx && keywest_ctx == i2c) {
105 i2c_del_driver(&keywest_driver);
106 keywest_ctx = NULL;
107 }
108}
109
Bill Pemberton15afafc2012-12-06 12:35:23 -0500110int snd_pmac_tumbler_post_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 int err;
113
Takashi Iwai783eaf42006-09-17 22:00:51 +0200114 if (!keywest_ctx || !keywest_ctx->client)
115 return -ENXIO;
116
Takashi Iwaie73ad382021-06-08 16:05:38 +0200117 err = keywest_ctx->init_client(keywest_ctx);
118 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 snd_printk(KERN_ERR "tumbler: %i :cannot initialize the MCS\n", err);
120 return err;
121 }
122 return 0;
123}
124
125/* exported */
Bill Pemberton15afafc2012-12-06 12:35:23 -0500126int snd_pmac_keywest_init(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Wolfram Sangac397c82015-05-09 19:42:22 +0200128 struct i2c_adapter *adap;
129 int err, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (keywest_ctx)
132 return -EBUSY;
133
Wolfram Sangac397c82015-05-09 19:42:22 +0200134 adap = i2c_get_adapter(0);
135 if (!adap)
136 return -EPROBE_DEFER;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 keywest_ctx = i2c;
139
Takashi Iwaie73ad382021-06-08 16:05:38 +0200140 err = i2c_add_driver(&keywest_driver);
141 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 snd_printk(KERN_ERR "cannot register keywest i2c driver\n");
Wolfram Sangac397c82015-05-09 19:42:22 +0200143 i2c_put_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return err;
145 }
Wolfram Sangac397c82015-05-09 19:42:22 +0200146
Wolfram Sang28760c12015-05-23 18:15:11 +0200147 /* There was already a device from i2c-powermac. Great, let's return */
148 if (keywest_probed)
149 return 0;
150
Wolfram Sangac397c82015-05-09 19:42:22 +0200151 /* We assume Macs have consecutive I2C bus numbers starting at 0 */
152 while (adap) {
Wolfram Sang28760c12015-05-23 18:15:11 +0200153 /* Scan for devices to be bound to */
Wolfram Sangac397c82015-05-09 19:42:22 +0200154 err = keywest_attach_adapter(adap);
155 if (!err)
156 return 0;
157 i2c_put_adapter(adap);
158 adap = i2c_get_adapter(++i);
159 }
160
161 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}